diff --git a/src/plugin/relativeTime/index.js b/src/plugin/relativeTime/index.js index 022fbfbd8..e11b67a8e 100644 --- a/src/plugin/relativeTime/index.js +++ b/src/plugin/relativeTime/index.js @@ -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) diff --git a/test/plugin/relativeTime.test.js b/test/plugin/relativeTime.test.js index 19994381f..0ef8e796a 100644 --- a/test/plugin/relativeTime.test.js +++ b/test/plugin/relativeTime.test.js @@ -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' @@ -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') +})