Skip to content

Commit 402b603

Browse files
xvaaraiamkun
authored andcommitted
fix: customParseFormat plugin supports parsing localizedFormats (#1110)
1 parent a3106a8 commit 402b603

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

src/plugin/customParseFormat/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { u } from '../localizedFormat/utils'
2+
13
const formattingTokens = /(\[[^[]*\])|([-:/.()\s]+)|(A|a|YYYY|YY?|MM?M?M?|Do|DD?|hh?|HH?|mm?|ss?|S{1,3}|z|ZZ?)/g
24

35
const match1 = /\d/ // 0 - 9
@@ -118,6 +120,7 @@ function correctHours(time) {
118120
}
119121

120122
function makeParser(format) {
123+
format = u(format, locale.formats)
121124
const array = format.match(formattingTokens)
122125
const { length } = array
123126
for (let i = 0; i < length; i += 1) {

src/plugin/localizedFormat/index.js

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,14 @@
11
import { FORMAT_DEFAULT } from '../../constant'
2-
import { t } from './utils'
2+
import { u, englishFormats } from './utils'
33

44
export default (o, c, d) => {
55
const proto = c.prototype
66
const oldFormat = proto.format
7-
const englishFormats = {
8-
LTS: 'h:mm:ss A',
9-
LT: 'h:mm A',
10-
L: 'MM/DD/YYYY',
11-
LL: 'MMMM D, YYYY',
12-
LLL: 'MMMM D, YYYY h:mm A',
13-
LLLL: 'dddd, MMMM D, YYYY h:mm A'
14-
}
7+
158
d.en.formats = englishFormats
169
proto.format = function (formatStr = FORMAT_DEFAULT) {
1710
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-
})
11+
const result = u(formatStr, formats)
2212
return oldFormat.call(this, result)
2313
}
2414
}

src/plugin/localizedFormat/utils.js

+13
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,16 @@
22
export const t = format =>
33
format.replace(/(\[[^\]]+])|(MMMM|MM|DD|dddd)/g, (_, a, b) => a || b.slice(1))
44

5+
export const englishFormats = {
6+
LTS: 'h:mm:ss A',
7+
LT: 'h:mm A',
8+
L: 'MM/DD/YYYY',
9+
LL: 'MMMM D, YYYY',
10+
LLL: 'MMMM D, YYYY h:mm A',
11+
LLLL: 'dddd, MMMM D, YYYY h:mm A'
12+
}
13+
14+
export const u = (formatStr, formats) => formatStr.replace(/(\[[^\]]+])|(LTS?|l{1,4}|L{1,4})/g, (_, a, b) => {
15+
const B = b && b.toUpperCase()
16+
return a || formats[b] || englishFormats[b] || t(formats[B])
17+
})

test/plugin/customParseFormat.test.js

+17
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import MockDate from 'mockdate'
22
import moment from 'moment'
33
import dayjs from '../../src'
44
import customParseFormat from '../../src/plugin/customParseFormat'
5+
import localizedFormats from '../../src/plugin/localizedFormat'
56
import uk from '../../src/locale/uk'
67
import '../../src/locale/zh-cn'
78
import '../../src/locale/ru'
89

910
dayjs.extend(customParseFormat)
11+
dayjs.extend(localizedFormats)
1012

1113
beforeEach(() => {
1214
MockDate.set(new Date())
@@ -72,6 +74,21 @@ it('recognizes noon in small letters', () => {
7274
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
7375
})
7476

77+
describe('parse localizedFormats', () => {
78+
['zh-cn', 'ru', 'uk', 'en'].forEach((lo) => {
79+
it(`Locale: ${lo}`, () => {
80+
const input = '2018-05-02 01:02:03.004'
81+
dayjs.locale(lo)
82+
moment.locale(lo)
83+
const longDateFormats = ['LT', 'LTS', 'L', 'LL', 'l', 'll', 'lll', 'l LT', 'LL [l] LTS'] // TODO: fix LLL, LLLL and llll
84+
longDateFormats.forEach((f) => {
85+
const localizedInput = moment(input).format(f)
86+
expect(dayjs(localizedInput, f).valueOf()).toBe(moment(localizedInput, f).valueOf())
87+
})
88+
})
89+
})
90+
})
91+
7592
it('leaves non-token parts of the format intact', () => {
7693
const input = '2018-05-02 12:00 +0000 S:/-.() SS h '
7794
const format = 'YYYY-MM-DD HH:mm ZZ [S]:/-.()[ SS h ]'

0 commit comments

Comments
 (0)