Skip to content

Commit

Permalink
fix: Correct upgrading from the improved locale format to the ultimat…
Browse files Browse the repository at this point in the history
…e one
  • Loading branch information
prantlf committed Oct 14, 2018
1 parent 01eb448 commit e1a290e
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/plugin/relativeTime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}, {})
}
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/plugin/relativeTime/pluralRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 13 additions & 2 deletions test/locale/keys.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
107 changes: 97 additions & 10 deletions test/plugin/relativeTime.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit e1a290e

Please sign in to comment.