From 6545739d8a20ed71fb894e865ce2deb8a721ca8e Mon Sep 17 00:00:00 2001 From: EdgarsN Date: Mon, 9 Nov 2020 05:12:47 +0200 Subject: [PATCH 1/5] fix: Update lv (Latvian) locale relative time (#1192) --- src/locale/lv.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/locale/lv.js b/src/locale/lv.js index b06bd65a9..7f8009616 100644 --- a/src/locale/lv.js +++ b/src/locale/lv.js @@ -17,6 +17,21 @@ const locale = { LL: 'YYYY. [gada] D. MMMM', LLL: 'YYYY. [gada] D. MMMM, HH:mm', LLLL: 'YYYY. [gada] D. MMMM, dddd, HH:mm' + }, + relativeTime: { + future: 'pēc %s', + past: 'pirms %s', + s: 'dažām sekundēm', + m: 'minūtes', + mm: '%d minūtēm', + h: 'stundas', + hh: '%d stundām', + d: 'dienas', + dd: '%d dienām', + M: 'mēneša', + MM: '%d mēnešiem', + y: 'gada', + yy: '%d gadiem' } } From aa217bee0fde9864e629bfc3782664f9062fed84 Mon Sep 17 00:00:00 2001 From: Ryan N Salim Date: Mon, 9 Nov 2020 20:51:35 +0700 Subject: [PATCH 2/5] add function handling for future and past object --- src/plugin/relativeTime/index.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/plugin/relativeTime/index.js b/src/plugin/relativeTime/index.js index 022fbfbd8..47ce029a8 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) From 484558afb479a942165f6e0c66b9d2de6eaec84b Mon Sep 17 00:00:00 2001 From: Ryan N Salim Date: Tue, 10 Nov 2020 14:17:09 +0700 Subject: [PATCH 3/5] Add unit test --- test/plugin/relativeTime.test.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test/plugin/relativeTime.test.js b/test/plugin/relativeTime.test.js index 19994381f..c05ccef8d 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') +}) From f69d708298dc49798386f88dc8a7a9eb5e41d074 Mon Sep 17 00:00:00 2001 From: Ryan N Salim Date: Tue, 10 Nov 2020 14:18:26 +0700 Subject: [PATCH 4/5] cosmetic --- test/plugin/relativeTime.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/plugin/relativeTime.test.js b/test/plugin/relativeTime.test.js index c05ccef8d..0ef8e796a 100644 --- a/test/plugin/relativeTime.test.js +++ b/test/plugin/relativeTime.test.js @@ -145,7 +145,7 @@ it('Locale without relativeTime config fallback', () => { }).fromNow()).toEqual(expect.any(String)) }) -it('Past and Future keys should support function for additional processing', () => { +it('Past and Future keys should support function for additional processing', () => { dayjs.extend(updateLocale) dayjs.updateLocale('en', { relativeTime: { From ddbe33c75597b3702fc84632a1603f3b7e2cfeab Mon Sep 17 00:00:00 2001 From: Ryan N Salim Date: Tue, 10 Nov 2020 14:21:44 +0700 Subject: [PATCH 5/5] clean up --- src/plugin/relativeTime/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugin/relativeTime/index.js b/src/plugin/relativeTime/index.js index 47ce029a8..e11b67a8e 100644 --- a/src/plugin/relativeTime/index.js +++ b/src/plugin/relativeTime/index.js @@ -60,7 +60,7 @@ export default (o, c, d) => { } } if (withoutSuffix) return out - const pastOrFuture = (isFuture ? loc.future : loc.past) + const pastOrFuture = isFuture ? loc.future : loc.past if (typeof pastOrFuture === 'function') { return pastOrFuture(out) }