Skip to content

Commit

Permalink
fix: Update LocalizedFormat plugin lowercase formats logic (#557)
Browse files Browse the repository at this point in the history
  • Loading branch information
allmoviestvshowslistsfilmography28 authored and iamkun committed Apr 26, 2019
1 parent 1f89ee7 commit 656a993
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
19 changes: 8 additions & 11 deletions src/plugin/localizedFormat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,16 @@ export default (o, c, d) => {
L: 'MM/DD/YYYY',
LL: 'MMMM D, YYYY',
LLL: 'MMMM D, YYYY h:mm A',
LLLL: 'dddd, MMMM D, YYYY h:mm A',
l: 'M/D/YYYY',
ll: 'MMM D, YYYY',
lll: 'MMM D, YYYY h:mm A',
llll: 'ddd, MMM D, YYYY h:mm A'
LLLL: 'dddd, MMMM D, YYYY h:mm A'
}
d.en.formats = englishFormats
proto.format = function (formatStr) {
const locale = this.$locale()
const formats = locale.formats || {}
const str = formatStr || FORMAT_DEFAULT
const result = str.replace(/(\[[^\]]+])|(LTS?|l{1,4}|L{1,4})/g, (_, a, b) =>
a || formats[b] || englishFormats[b])
const t = format => format.replace(/(\[[^\]]+])|(MMMM|MM|DD|dddd)/g, (_, a, b) => a || b.slice(1))
proto.format = function (formatStr = FORMAT_DEFAULT) {
const { formats = {} } = this.$locale()
const result = formatStr.replace(/(\[[^\]]+])|(LTS?|l{1,4}|L{1,4})/g, (_, a, b) => {
const B = b && b.toUpperCase()
return a || formats[b] || englishFormats[b] || t(formats[B])
})
return oldFormat.call(this, result)
}
}
Expand Down
40 changes: 37 additions & 3 deletions test/plugin/localizedFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import es from '../../src/locale/es'
import znCn from '../../src/locale/zh-cn'
import localizedFormat from '../../src/plugin/localizedFormat'

dayjs.extend(localizedFormat)
Expand All @@ -17,7 +18,7 @@ afterEach(() => {
it('Declares English localized formats', () => {
expect(dayjs.en).toBeDefined()
expect(dayjs.en.formats).toBeDefined();
['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL', 'l', 'll', 'lll', 'llll'].forEach(option =>
['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL'].forEach(option =>
expect(dayjs.en.formats[option]).toBeDefined())
})

Expand All @@ -30,20 +31,35 @@ it('Should not interpolate characters inside square brackets', () => {
expect(actualDate.format('YYYY [l] YYYY')).toBe('1970 l 1970')
expect(actualDate.format('l [l] l')).toBe('1/1/1970 l 1/1/1970')
expect(actualDate.format('[L LL LLL LLLL]')).toBe(expectedDate.format('[L LL LLL LLLL]'))


const localeFormats = {
L: '[MMMM MM DD dddd]'
}
const mockedDayJsLocale = {
...es,
name: 'fake-locale',
formats: {
...localeFormats
}
}
const fakeDate = dayjs(date, { locale: mockedDayJsLocale })

expect(fakeDate.locale('fake-locale').format('l')).toEqual('MMMM MM DD dddd')
})

it('Recognizes localized format options', () => {
const { formats } = dayjs.en
const date = dayjs();
['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL', 'l', 'll', 'lll', 'llll'].forEach(option =>
['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL'].forEach(option =>
expect(date.format(option)).toBe(date.format(formats[option])))
})

it('Uses correct English formats', () => {
const date = new Date()
const actualDate = dayjs(date)
const expectedDate = moment(date);
['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL', 'l', 'll', 'lll', 'llll'].forEach(option =>
['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL'].forEach(option =>
expect(actualDate.format(option)).toBe(expectedDate.format(option)))
})

Expand Down Expand Up @@ -72,3 +88,21 @@ it('Uses the locale of the dayjs instance', () => {
const spanishDate = dayjs(date, { locale: es })
expect(englishDate.format('L LTS')).not.toBe(spanishDate.format('L LTS'))
})


it('Uses the localized lowercase formats if defined', () => {
const date = new Date()
const znDate = dayjs(date, { locale: znCn });
['l', 'll', 'lll', 'llll'].forEach(option => expect(znDate.format(option)).toBe(znDate.format(znCn.formats[option])))
})

it('Uses the localized uppercase formats as a base for lowercase formats, if not defined', () => {
const date = new Date()
const spanishDate = dayjs(date, { locale: es });

['l', 'll', 'lll', 'llll'].forEach((option) => {
const upperCaseFormat = es.formats[option.toUpperCase()]
const adaptedFormat = upperCaseFormat.replace(/(\[[^\]]+])|(MMMM|MM|DD|dddd)/g, (_, a, b) => a || b.slice(1))
expect(spanishDate.format(option)).toBe(spanishDate.format(adaptedFormat))
})
})

0 comments on commit 656a993

Please sign in to comment.