From e1a290e3d9e3bca8de8393e3e3bd6edf7fe4a43a Mon Sep 17 00:00:00 2001 From: Ferdinand Prantl Date: Sun, 14 Oct 2018 15:14:45 +0200 Subject: [PATCH] fix: Correct upgrading from the improved locale format to the ultimate one --- src/plugin/relativeTime/index.js | 8 +- src/plugin/relativeTime/pluralRules.js | 1 + test/locale/keys.test.js | 15 +++- test/plugin/relativeTime.test.js | 107 ++++++++++++++++++++++--- 4 files changed, 115 insertions(+), 16 deletions(-) diff --git a/src/plugin/relativeTime/index.js b/src/plugin/relativeTime/index.js index c41ea1c6a..5e1ac7e2e 100644 --- a/src/plugin/relativeTime/index.js +++ b/src/plugin/relativeTime/index.js @@ -2,10 +2,10 @@ import * as C from '../../constant' import pluralRules from './pluralRules' // Returns 0 for singular and 1 for plural for languages with a single plural -const simplePluralRule = pluralRules[1] +const simplePluralRule = 1 // Returns 0 for singular, 1 for plural for 2 <= value <= 4 and 2 for plural // for value >= 5, which is sufficient for some languages like Czech -const improvedPluralRule = pluralRules[8] +const improvedPluralRule = 8 export default (o, c, d) => { const proto = c.prototype @@ -81,7 +81,7 @@ export default (o, c, d) => { const pluralForms = result[unit] || (result[unit] = []) // Make sure, that singular comes before plurals and plurals come in // the right order - pluralForms[kl - 1] = loc[key] + pluralForms[kl - 1] = object[key] return result }, {}) } @@ -160,7 +160,7 @@ export default (o, c, d) => { } pluralFormIndex = pluralRule(abs) } else { - // Singluar is alway sthe first item in the array + // Singular is always the first item in the array pluralFormIndex = 0 } out = pluralForms[pluralFormIndex].replace('%d', abs) diff --git a/src/plugin/relativeTime/pluralRules.js b/src/plugin/relativeTime/pluralRules.js index 5b84a06f7..aae193b59 100644 --- a/src/plugin/relativeTime/pluralRules.js +++ b/src/plugin/relativeTime/pluralRules.js @@ -2,6 +2,7 @@ // See the expressions to identify the plural form at http://docs.translatehouse.org/projects/localization-guide/en/latest/l10n/pluralforms.html#pluralforms-list /* eslint-disable no-confusing-arrow, no-nested-ternary */ +/* istanbul ignore file */ export default [ // Plural rule #0 (1 form) diff --git a/test/locale/keys.test.js b/test/locale/keys.test.js index e4d54bc80..88fb48183 100644 --- a/test/locale/keys.test.js +++ b/test/locale/keys.test.js @@ -41,15 +41,26 @@ it('Locale keys', () => { expect(Object.keys(formats).sort()).toEqual(['L', 'LL', 'LLL', 'LLLL', 'LT', 'LTS'].sort()) } if (relativeTime) { - // Old locale object structure if (relativeTime.s) { + // Old locale object structure expect(Object.keys(relativeTime).sort()) .toEqual(['d', 'dd', 'future', 'h', 'hh', 'm', 'mm', 'M', 'MM', 'past', 's', 'y', 'yy'].sort()) expect(Object.keys(relativeTime).every(key => // eslint-disable-next-line implicit-arrow-linebreak typeof relativeTime[key] === 'string')).toBeTruthy() + } else if (!relativeTime.pluralRule) { + // Improved locale object structure + expect(Object.keys(relativeTime).sort()).toEqual(['duration', 'future', 'past'].sort()); + ['duration', 'future', 'past'].forEach(key => + expect(Object.keys(relativeTime[key]).sort()) + .toEqual(['d', 'dd', 'ddd', 'h', 'hh', 'hhh', 'm', 'mm', 'mmm', + 'M', 'MM', 'MMM', 's', 'y', 'yy', 'yyy'].sort())); + ['duration', 'future', 'past'].forEach(key => + Object.keys(relativeTime[key]).forEach(key2 => + // eslint-disable-next-line implicit-arrow-linebreak + expect(typeof relativeTime[key][key2]).toEqual('string'))) } else { - // New locale object structure + // Ultimate locale object structure expect(Object.keys(relativeTime).sort()).toEqual(['duration', 'future', 'past', 'pluralRule'].sort()); ['duration', 'future', 'past'].forEach(key => // eslint-disable-next-line implicit-arrow-linebreak diff --git a/test/plugin/relativeTime.test.js b/test/plugin/relativeTime.test.js index ae6d57758..0793ea0c8 100644 --- a/test/plugin/relativeTime.test.js +++ b/test/plugin/relativeTime.test.js @@ -14,17 +14,104 @@ afterEach(() => { }) it('Upgrades old locale objects', () => { - // English is the default locale used for tests; - // initially it is defined by the old locale structure - expect(dayjs.en).toBeDefined() - expect(dayjs.en.relativeTime).toBeDefined() - expect(dayjs.en.relativeTime.s).toBeDefined() - expect(dayjs.en.relativeTime.duration).toBeUndefined() + const old = { + name: 'old-relativeTime', + relativeTime: { + future: 'in %s', + past: '%s ago', + s: 'a few seconds', + m: 'a minute', + mm: '%d minutes', + h: 'an hour', + hh: '%d hours', + d: 'a day', + dd: '%d days', + M: 'a month', + MM: '%d months', + y: 'a year', + yy: '%d years' + } + } + dayjs.locale(old, null, true) + const locale = dayjs(undefined, { locale: 'old-relativeTime' }).$locale() // Call the plugin to upgrade the locale structure on the fly - dayjs().from(dayjs()) - // English locale has been upgraded to the new locale structure - expect(dayjs.en.relativeTime.s).toBeUndefined() - expect(dayjs.en.relativeTime.duration).toBeDefined() + dayjs(undefined, { locale: 'old-relativeTime' }).fromNow() + // The locale has been upgraded to the new locale structure + expect(locale.relativeTime.s).toBeUndefined() + expect(typeof locale.relativeTime.duration).toEqual('object') + expect(Array.isArray([locale.relativeTime.duration.s])).toBeTruthy() + expect(typeof locale.relativeTime.duration.s[0]).toEqual('string') + expect(typeof locale.relativeTime.pluralRule).toEqual('number') +}) + +it('Upgrades improved locale objects', () => { + const improved = { + name: 'improved-relativeTime', + relativeTime: { + duration: { + s: 'několik sekund', + m: 'minuta', + mm: '%d minuty', + mmm: '%d minut', + h: 'hodina', + hh: '%d hodiny', + hhh: '%d hodin', + d: 'den', + dd: '%d dny', + ddd: '%d dní', + M: 'měsíc', + MM: '%d měsíce', + MMM: '%d měsícú', + y: 'rok', + yy: '%d roky', + yyy: '%d let' + }, + future: { + s: 'za několik sekund', + m: 'za minutu', + mm: 'za %d minuty', + mmm: 'za %d minut', + h: 'za hodinu', + hh: 'za %d hodiny', + hhh: 'za %d hodin', + d: 'zítra', + dd: 'za %d dny', + ddd: 'za %d dní', + M: 'za měsíc', + MM: 'za %d měsíce', + MMM: 'za %d měsícú', + y: 'za rok', + yy: 'za %d roky', + yyy: 'za %d let' + }, + past: { + s: 'před několika sekundami', + m: 'před minutou', + mm: 'před %d minutami', + mmm: 'před %d minutami', + h: 'před hodinou', + hh: 'před %d hodinami', + hhh: 'před %d hodinami', + d: 'včera', + dd: 'před %d dny', + ddd: 'před %d dny', + M: 'před měsícem', + MM: 'před %d měsíci', + MMM: 'před %d měsíci', + y: 'vloni', + yy: 'před %d roky', + yyy: 'před %d lety' + } + } + } + dayjs.locale(improved, null, true) + const locale = dayjs(undefined, { locale: 'improved-relativeTime' }).$locale() + // Call the plugin to upgrade the locale structure on the fly + dayjs(undefined, { locale: 'improved-relativeTime' }).fromNow() + // The locale has been upgraded to the new locale structure + expect(Array.isArray([locale.relativeTime.duration.s])).toBeTruthy() + expect(typeof locale.relativeTime.duration.s[0]).toEqual('string') + expect(typeof locale.relativeTime.pluralRule).toEqual('number') }) it('Time from X', () => {