From 9aef332f7ce4727d80bace6feb4c6a96ce52c419 Mon Sep 17 00:00:00 2001 From: HarlesPilter Date: Thu, 30 Jan 2020 12:30:48 +0200 Subject: [PATCH 1/4] feat: Add correct Estonian relativeTime locale for past and future tense --- src/locale/et.js | 63 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 19 deletions(-) diff --git a/src/locale/et.js b/src/locale/et.js index 82422bbf4..1bc8f2e17 100644 --- a/src/locale/et.js +++ b/src/locale/et.js @@ -1,6 +1,39 @@ // Estonian [et] import dayjs from 'dayjs' +function relativeTimeWithTense(number, withoutSuffix, key, isFuture) { + const pastTense = { + s: 'mõni sekund', + m: 'minut', + mm: '%d minutit', + h: 'tund', + hh: '%d tundi', + d: 'päev', + dd: '%d päeva', + M: 'kuu', + MM: '%d kuud', + y: 'aasta', + yy: '%d aastat' + } + + // in Estonian future tenses don't care about plural + const futureTense = { + s: 'mõne sekundi', + m: 'minuti', + mm: '%d minuti', + h: 'tunni', + hh: '%d tunni', + d: 'päeva', + dd: '%d päeva', + M: 'kuu', + MM: '%d kuu', + y: 'aasta', + yy: '%d aasta' + } + + return (isFuture ? futureTense : pastTense)[key].replace('%d', number) +} + const locale = { name: 'et', // Estonian weekdays: 'pühapäev_esmaspäev_teisipäev_kolmapäev_neljapäev_reede_laupäev'.split('_'), // Note weekdays are not capitalized in Estonian @@ -10,28 +43,20 @@ const locale = { monthsShort: 'jaan_veebr_märts_apr_mai_juuni_juuli_aug_sept_okt_nov_dets'.split('_'), ordinal: n => `${n}.`, weekStart: 1, - /* - * This relativeTime is currently configured for having proper past - * tense forms since Estonian needs a separate version for future tense - * and I think past tense is a more common use case for this kind of library. - * - * Doing this properly requires this issue to be fixed: - * https://github.com/iamkun/dayjs/issues/302 - */ relativeTime: { future: '%s pärast', past: '%s tagasi', - s: 'mõni sekund', // for past tense - m: 'minut', // for past tense - mm: '%d minutit', // for past tense - h: 'tund', // for past tense - hh: '%d tundi', // for past tense - d: 'päev', // for past tense - dd: '%d päeva', // for past tense - M: 'kuu', // for past tense - MM: '%d kuud', // for past tense - y: 'aasta', // for past tense - yy: '%d aastat' // for past tense + s: relativeTimeWithTense, + m: relativeTimeWithTense, + mm: relativeTimeWithTense, + h: relativeTimeWithTense, + hh: relativeTimeWithTense, + d: relativeTimeWithTense, + dd: relativeTimeWithTense, + M: relativeTimeWithTense, + MM: relativeTimeWithTense, + y: relativeTimeWithTense, + yy: relativeTimeWithTense }, formats: { LT: 'H:mm', From 1ae7d91be348631b36f0d9b5b0b5dec0e620f491 Mon Sep 17 00:00:00 2001 From: HarlesPilter Date: Thu, 30 Jan 2020 13:28:26 +0200 Subject: [PATCH 2/4] feat: Refactor code to match more the moment implementation --- src/locale/et.js | 45 ++++++++++++++++----------------------------- 1 file changed, 16 insertions(+), 29 deletions(-) diff --git a/src/locale/et.js b/src/locale/et.js index 1bc8f2e17..bda79c6e0 100644 --- a/src/locale/et.js +++ b/src/locale/et.js @@ -2,36 +2,23 @@ import dayjs from 'dayjs' function relativeTimeWithTense(number, withoutSuffix, key, isFuture) { - const pastTense = { - s: 'mõni sekund', - m: 'minut', - mm: '%d minutit', - h: 'tund', - hh: '%d tundi', - d: 'päev', - dd: '%d päeva', - M: 'kuu', - MM: '%d kuud', - y: 'aasta', - yy: '%d aastat' + const format = { + s: ['mõne sekundi', 'mõni sekund', 'paar sekundit'], + ss: ['%d sekundi', '%d sekundit'], + m: ['ühe minuti', 'üks minut'], + mm: ['%d minuti', '%d minutit'], + h: ['ühe tunni', 'tund aega', 'üks tund'], + hh: ['%d tunni', '%d tundi'], + d: ['ühe päeva', 'üks päev'], + M: ['kuu aja', 'kuu aega', 'üks kuu'], + MM: ['%d kuu', '%d kuud'], + y: ['ühe aasta', 'aasta', 'üks aasta'], + yy: ['%d aasta', '%d aastat'] } - - // in Estonian future tenses don't care about plural - const futureTense = { - s: 'mõne sekundi', - m: 'minuti', - mm: '%d minuti', - h: 'tunni', - hh: '%d tunni', - d: 'päeva', - dd: '%d päeva', - M: 'kuu', - MM: '%d kuu', - y: 'aasta', - yy: '%d aasta' + if (withoutSuffix) { + return (format[key][2] ? format[key][2] : format[key][1]).replace('%d', number) } - - return (isFuture ? futureTense : pastTense)[key].replace('%d', number) + return (isFuture ? format[key][0] : format[key][1]).replace('%d', number) } const locale = { @@ -52,7 +39,7 @@ const locale = { h: relativeTimeWithTense, hh: relativeTimeWithTense, d: relativeTimeWithTense, - dd: relativeTimeWithTense, + dd: '%d päeva', M: relativeTimeWithTense, MM: relativeTimeWithTense, y: relativeTimeWithTense, From c720edc97fe1c78423c09bc57a4e8ad70031e784 Mon Sep 17 00:00:00 2001 From: HarlesPilter Date: Thu, 30 Jan 2020 13:28:53 +0200 Subject: [PATCH 3/4] feat: Add tests for et locale --- test/locale/et.test.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 test/locale/et.test.js diff --git a/test/locale/et.test.js b/test/locale/et.test.js new file mode 100644 index 000000000..5b8b75833 --- /dev/null +++ b/test/locale/et.test.js @@ -0,0 +1,38 @@ +import moment from 'moment' +import MockDate from 'mockdate' +import dayjs from '../../src' +import relativeTime from '../../src/plugin/relativeTime' +import '../../src/locale/et' + +dayjs.extend(relativeTime) + +beforeEach(() => { + MockDate.set(new Date()) +}) + +afterEach(() => { + MockDate.reset() +}) + +it('RelativeTime: Time from X', () => { + const T = [ + [44.4, 'second'], // a few seconds + [89.5, 'second'], // a minute + [43, 'minute'], // 44 minutes + [21, 'hour'], // 21 hours + [25, 'day'], // 25 days + [10, 'month'], // 2 month + [18, 'month'] // 2 years + ] + + T.forEach((t) => { + dayjs.locale('et') + moment.locale('et') + expect(dayjs().from(dayjs().add(t[0], t[1]))) + .toBe(moment().from(moment().add(t[0], t[1]))) + expect(dayjs().from(dayjs().subtract(t[0], t[1]))) + .toBe(moment().from(moment().subtract(t[0], t[1]))) + expect(dayjs().from(dayjs().add(t[0], t[1]), true)) + .toBe(moment().from(moment().add(t[0], t[1]), true)) + }) +}) From 33905ae775c3aed2819cc461d2f613d0e0445d7c Mon Sep 17 00:00:00 2001 From: HarlesPilter Date: Thu, 30 Jan 2020 14:27:33 +0200 Subject: [PATCH 4/4] fix: Remove unnecessary line --- src/locale/et.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/locale/et.js b/src/locale/et.js index bda79c6e0..8a9ad1e70 100644 --- a/src/locale/et.js +++ b/src/locale/et.js @@ -4,7 +4,6 @@ import dayjs from 'dayjs' function relativeTimeWithTense(number, withoutSuffix, key, isFuture) { const format = { s: ['mõne sekundi', 'mõni sekund', 'paar sekundit'], - ss: ['%d sekundi', '%d sekundit'], m: ['ühe minuti', 'üks minut'], mm: ['%d minuti', '%d minutit'], h: ['ühe tunni', 'tund aega', 'üks tund'],