Skip to content

Commit

Permalink
fix: Add function handling for relativeTime.future and relativeTime.p…
Browse files Browse the repository at this point in the history
…ast (#1197)
  • Loading branch information
drgx authored Nov 12, 2020
1 parent b56936a commit ef1979c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/plugin/relativeTime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ export default (o, c, d) => {
}
}
if (withoutSuffix) return out
return (isFuture ? loc.future : loc.past).replace('%s', out)
const pastOrFuture = isFuture ? loc.future : loc.past
if (typeof pastOrFuture === 'function') {
return pastOrFuture(out)
}
return pastOrFuture.replace('%s', out)
}
proto.to = function (input, withoutSuffix) {
return fromTo(input, withoutSuffix, this, true)
Expand Down
28 changes: 28 additions & 0 deletions test/plugin/relativeTime.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import moment from 'moment'
import dayjs from '../../src'
import * as C from '../../src/constant'
import relativeTime from '../../src/plugin/relativeTime'
import updateLocale from '../../src/plugin/updateLocale'
import utc from '../../src/plugin/utc'
import '../../src/locale/ru'

Expand Down Expand Up @@ -143,3 +144,30 @@ it('Locale without relativeTime config fallback', () => {
name: 'test-locale'
}).fromNow()).toEqual(expect.any(String))
})

it('Past and Future keys should support function for additional processing', () => {
dayjs.extend(updateLocale)
dayjs.updateLocale('en', {
relativeTime: {
future: input => `${input} modified`,
past: input => `${input} modified`,
s: 'just now',
m: ' 1 min',
mm: '%d min',
h: '1 hr',
hh: '%d hrs',
d: 'a day',
dd: '%d days',
M: 'a month',
MM: '%d months',
y: 'a year',
yy: '%d years'
}
})


const past = Date.now() - 1000
expect(dayjs(past).fromNow()).toEqual(' 1 min modified')
const future = Date.now() + 1000
expect(dayjs(future).fromNow()).toEqual(' 1 min modified')
})

0 comments on commit ef1979c

Please sign in to comment.