Skip to content

Commit d409304

Browse files
mariomciamkun
authored andcommitted
fix: Update LocalizedFormat plugin lowercase formats logic (#557)
1 parent 9279718 commit d409304

File tree

2 files changed

+45
-14
lines changed

2 files changed

+45
-14
lines changed

src/plugin/localizedFormat/index.js

+8-11
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,16 @@ export default (o, c, d) => {
99
L: 'MM/DD/YYYY',
1010
LL: 'MMMM D, YYYY',
1111
LLL: 'MMMM D, YYYY h:mm A',
12-
LLLL: 'dddd, MMMM D, YYYY h:mm A',
13-
l: 'M/D/YYYY',
14-
ll: 'MMM D, YYYY',
15-
lll: 'MMM D, YYYY h:mm A',
16-
llll: 'ddd, MMM D, YYYY h:mm A'
12+
LLLL: 'dddd, MMMM D, YYYY h:mm A'
1713
}
1814
d.en.formats = englishFormats
19-
proto.format = function (formatStr) {
20-
const locale = this.$locale()
21-
const formats = locale.formats || {}
22-
const str = formatStr || FORMAT_DEFAULT
23-
const result = str.replace(/(\[[^\]]+])|(LTS?|l{1,4}|L{1,4})/g, (_, a, b) =>
24-
a || formats[b] || englishFormats[b])
15+
const t = format => format.replace(/(\[[^\]]+])|(MMMM|MM|DD|dddd)/g, (_, a, b) => a || b.slice(1))
16+
proto.format = function (formatStr = FORMAT_DEFAULT) {
17+
const { formats = {} } = this.$locale()
18+
const result = formatStr.replace(/(\[[^\]]+])|(LTS?|l{1,4}|L{1,4})/g, (_, a, b) => {
19+
const B = b && b.toUpperCase()
20+
return a || formats[b] || englishFormats[b] || t(formats[B])
21+
})
2522
return oldFormat.call(this, result)
2623
}
2724
}

test/plugin/localizedFormat.test.js

+37-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import MockDate from 'mockdate'
22
import moment from 'moment'
33
import dayjs from '../../src'
44
import es from '../../src/locale/es'
5+
import znCn from '../../src/locale/zh-cn'
56
import localizedFormat from '../../src/plugin/localizedFormat'
67

78
dayjs.extend(localizedFormat)
@@ -17,7 +18,7 @@ afterEach(() => {
1718
it('Declares English localized formats', () => {
1819
expect(dayjs.en).toBeDefined()
1920
expect(dayjs.en.formats).toBeDefined();
20-
['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL', 'l', 'll', 'lll', 'llll'].forEach(option =>
21+
['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL'].forEach(option =>
2122
expect(dayjs.en.formats[option]).toBeDefined())
2223
})
2324

@@ -30,20 +31,35 @@ it('Should not interpolate characters inside square brackets', () => {
3031
expect(actualDate.format('YYYY [l] YYYY')).toBe('1970 l 1970')
3132
expect(actualDate.format('l [l] l')).toBe('1/1/1970 l 1/1/1970')
3233
expect(actualDate.format('[L LL LLL LLLL]')).toBe(expectedDate.format('[L LL LLL LLLL]'))
34+
35+
36+
const localeFormats = {
37+
L: '[MMMM MM DD dddd]'
38+
}
39+
const mockedDayJsLocale = {
40+
...es,
41+
name: 'fake-locale',
42+
formats: {
43+
...localeFormats
44+
}
45+
}
46+
const fakeDate = dayjs(date, { locale: mockedDayJsLocale })
47+
48+
expect(fakeDate.locale('fake-locale').format('l')).toEqual('MMMM MM DD dddd')
3349
})
3450

3551
it('Recognizes localized format options', () => {
3652
const { formats } = dayjs.en
3753
const date = dayjs();
38-
['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL', 'l', 'll', 'lll', 'llll'].forEach(option =>
54+
['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL'].forEach(option =>
3955
expect(date.format(option)).toBe(date.format(formats[option])))
4056
})
4157

4258
it('Uses correct English formats', () => {
4359
const date = new Date()
4460
const actualDate = dayjs(date)
4561
const expectedDate = moment(date);
46-
['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL', 'l', 'll', 'lll', 'llll'].forEach(option =>
62+
['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL'].forEach(option =>
4763
expect(actualDate.format(option)).toBe(expectedDate.format(option)))
4864
})
4965

@@ -72,3 +88,21 @@ it('Uses the locale of the dayjs instance', () => {
7288
const spanishDate = dayjs(date, { locale: es })
7389
expect(englishDate.format('L LTS')).not.toBe(spanishDate.format('L LTS'))
7490
})
91+
92+
93+
it('Uses the localized lowercase formats if defined', () => {
94+
const date = new Date()
95+
const znDate = dayjs(date, { locale: znCn });
96+
['l', 'll', 'lll', 'llll'].forEach(option => expect(znDate.format(option)).toBe(znDate.format(znCn.formats[option])))
97+
})
98+
99+
it('Uses the localized uppercase formats as a base for lowercase formats, if not defined', () => {
100+
const date = new Date()
101+
const spanishDate = dayjs(date, { locale: es });
102+
103+
['l', 'll', 'lll', 'llll'].forEach((option) => {
104+
const upperCaseFormat = es.formats[option.toUpperCase()]
105+
const adaptedFormat = upperCaseFormat.replace(/(\[[^\]]+])|(MMMM|MM|DD|dddd)/g, (_, a, b) => a || b.slice(1))
106+
expect(spanishDate.format(option)).toBe(spanishDate.format(adaptedFormat))
107+
})
108+
})

0 commit comments

Comments
 (0)