/
customParseFormat.test.js
188 lines (157 loc) · 5.92 KB
/
customParseFormat.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import customParseFormat from '../../src/plugin/customParseFormat'
import uk from '../../src/locale/uk'
import '../../src/locale/zh-cn'
dayjs.extend(customParseFormat)
beforeEach(() => {
MockDate.set(new Date())
})
afterEach(() => {
MockDate.reset()
})
it('does not break the built-in parsing', () => {
const input = '2018-05-02 01:02:03.004'
expect(dayjs(input).valueOf()).toBe(moment(input).valueOf())
})
it('parse padded string', () => {
const input = '2018-05-02 01:02:03.004 AM +01:00'
const format = 'YYYY-MM-DD HH:mm:ss.SSS A Z'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})
it('parse string January (getMonth() = 0)', () => {
const input = '01/01/2019'
const format = 'DD/MM/YYYY'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})
it('parse unpadded string', () => {
const input = '2.5.18 1:2:3.4 PM -0100'
const format = 'D.M.YY H:m:s.S A ZZ'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})
it('parse time zone abbreviation', () => {
const input = '05/02/69 1:02:03.004 AM +01:00 (CET)'
const format = 'MM/DD/YY h:mm:ss.SSS A Z (z)'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})
it('parse time zone abbreviation2', () => {
const input = '05/02/69 1:02:03.04 AM +01:00 (CET)'
const format = 'MM/DD/YY h:mm:ss.SS A Z (z)'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})
it('recognizes midnight in small letters', () => {
const input = '2018-05-02 12:00 am'
const format = 'YYYY-MM-DD hh:mm a'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})
it('recognizes noon in small letters', () => {
const input = '2018-05-02 12:00 pm'
const format = 'YYYY-MM-DD hh:mm a'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})
it('leaves non-token parts of the format intact', () => {
const input = '2018-05-02 12:00 +0000 S:/-.() SS h '
const format = 'YYYY-MM-DD HH:mm ZZ [S]:/-.()[ SS h ]'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})
it('timezone with no hour', () => {
const input = '2018-05-02 +0000'
const format = 'YYYY-MM-DD ZZ'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})
it('parse hh:mm', () => {
const input = '12:00'
const format = 'hh:mm'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})
it('parse HH:mm:ss', () => {
const input = '00:27:21'
const format = 'HH:mm:ss'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})
it('parse HH:mm:ss but only one digit', () => {
const input = '0:0:1'
const format = 'HH:mm:ss'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})
it('parse hh:mm:ss but only one digit', () => {
const input = '0:0:1'
const format = 'hh:mm:ss'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})
it('fails with an invalid format', () => {
const input = '2018-05-02 12:00 PM'
const format = 'C'
expect(dayjs(input, format).format().toLowerCase())
.toBe(moment(input, format).format().toLowerCase())
})
it('parse month from string', () => {
const input = '2018 February 03'
const format = 'YYYY MMMM DD'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})
it('parse month from short string', () => {
const input = '2018 Feb 03'
const format = 'YYYY MMM DD'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})
it('parse month from string with locale in config', () => {
const input = '2018 лютий 03'
const format = 'YYYY MMMM DD'
expect(dayjs(input, { format, locale: uk }).valueOf()).toBe(moment(input, format, 'uk').valueOf())
})
it('parse month from short string with locale in config', () => {
const input = '2018 трав 03'
const format = 'YYYY MMM DD'
expect(dayjs(input, { format, locale: uk }).valueOf()).toBe(moment(input, format, 'uk').valueOf())
})
it('parse month from short string with locale in argument', () => {
const input = '2018 трав 03'
const format = 'YYYY MMM DD'
expect(dayjs(input, format, 'uk').valueOf()).toBe(moment(input, format, 'uk').valueOf())
})
it('parse month from string with locale in argument', () => {
const input = '2018 лютий 03'
const format = 'YYYY MMMM DD'
expect(dayjs(input, format, 'uk').valueOf()).toBe(moment(input, format, 'uk').valueOf())
})
it('return Invalid Date when parse corrupt string', () => {
const input = '2018 Turnip 03'
const format = 'YYYY MMMM DD'
expect(dayjs(input, format).format()).toBe('Invalid Date')
})
it('return Invalid Date when parse corrupt short string', () => {
const input = '2018 Dog 03'
const format = 'YYYY MMM DD'
expect(dayjs(input, format).format()).toBe('Invalid Date')
})
it('correctly parse month from string after changing locale globally', () => {
const input = '2018 лютий 03'
const format = 'YYYY MMMM DD'
const dayjsLocale = dayjs().$locale()
const momentLocale = moment.locale()
try {
dayjs.locale(uk)
moment.locale('uk')
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
} finally {
dayjs.locale(dayjsLocale)
moment.locale(momentLocale)
}
})
it('correctly parse ordinal', () => {
const input = '7th March 2019'
const input2 = '17th March 2019'
const inputFalse = '7st March 2019'
const inputZHCN = '7日 三月 2019'
const format = 'Do MMMM YYYY'
expect(dayjs(input, format).valueOf())
.toBe(moment(input, format).valueOf())
expect(dayjs(input2, format).valueOf())
.toBe(moment(input2, format).valueOf())
expect(dayjs(inputFalse, format).valueOf())
.toBe(moment(inputFalse, format).valueOf())
expect(dayjs(inputZHCN, format, 'zh-cn').valueOf())
.toBe(moment(inputZHCN, format, 'zh-cn').valueOf())
})