deno.land / x / dayjs@v1.11.5 / test / parse.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import moment from 'moment'import MockDate from 'mockdate'import dayjs from '../src'import { REGEX_PARSE } from '../src/constant'
beforeEach(() => { MockDate.set(new Date())})
afterEach(() => { MockDate.reset()})
describe('Parse', () => { it('Now', () => { expect(dayjs().valueOf()).toBe(moment().valueOf()) })
it('moment-js like formatted dates', () => { global.console.warn = jest.genMockFunction()// moment.js '2018-4-1 1:1:1:22' will throw warn let d = '20130108' expect(dayjs(d).valueOf()).toBe(moment(d).valueOf()) d = '2018-04-24' expect(dayjs(d).valueOf()).toBe(moment(d).valueOf()) d = '2018-04-24 11:12' expect(dayjs(d).format()).toBe(moment(d).format()) // not recommend d = '2018-05-02 11:12:13' expect(dayjs(d).valueOf()).toBe(moment(d).valueOf()) d = '2018-05-02 11:12:13.998' expect(dayjs(d).valueOf()).toBe(moment(d).valueOf()) d = '2018-4-1' expect(dayjs(d).valueOf()).toBe(moment(d).valueOf()) // not recommend d = '2018-4-1 11:12' expect(dayjs(d).format()).toBe(moment(d).format()) // not recommend d = '2018-4-1 1:1:1:223' expect(dayjs(d).valueOf()).toBe(moment(d).valueOf()) // not recommend d = '2018-01' expect(dayjs(d).valueOf()).toBe(moment(d).valueOf()) // not recommend d = '2018' expect(dayjs(d).format()).toBe(moment(d).format()) // not recommend d = '2018-05-02T11:12:13Z' // should go direct to new Date() rather our regex expect(dayjs(d).format()).toBe(moment(d).format()) // not recommend })
it('String ISO 8601 date, time and zone', () => { const time = '2018-04-04T16:00:00.000Z' expect(dayjs(time).valueOf()).toBe(moment(time).valueOf()) })
it('String RFC 2822, time and zone', () => { const time = 'Mon, 11 Feb 2019 09:46:50 GMT+1' const expected = '2019-02-11T08:46:50.000Z' const d = dayjs(time) expect(d.toISOString()).toEqual(expected) expect(d.valueOf()).toBe(moment(time).valueOf()) })
it('String ECMAScript, time and zone', () => { // should parse dates formatted in ECMA script format // see https://www.ecma-international.org/ecma-262/9.0/index.html#sec-date.prototype.tostring const time = 'Mon Feb 11 2019 11:01:37 GMT+0100 (Mitteleuropäische Normalzeit)' const expected = '2019-02-11T10:01:37.000Z' const d = dayjs(time) expect(d.toISOString()).toEqual(expected) expect(d.valueOf()).toBe(moment(time).valueOf()) })
it('rejects invalid values', () => { expect(dayjs({}).isValid()).toBe(false) expect(dayjs(() => '2018-01-01').isValid()).toBe(false) expect(dayjs(Infinity).isValid()).toBe(false) expect(dayjs(NaN).isValid()).toBe(false) expect(dayjs([2018, 5, 1, 13, 52, 44]).isValid()).toBe(false) // Arrays with time part })
it('parses Arrays with date part', () => { const dateParts = [2018, 5, 1] const expected = '2018-05-01T00:00:00.000Z' const d = dayjs(dateParts) const normalized = d.add(d.utcOffset(), 'minutes') // make test run in every timezone expect(normalized.toISOString()).toEqual(expected) })
it('parses unlimited millisecond', () => { const date = '2019-03-25T06:41:00.999999999' const ds = dayjs(date) const ms = moment(date) expect(ds.valueOf()).toEqual(ms.valueOf()) expect(ds.millisecond()).toEqual(ms.millisecond()) })
it('String Other, Undefined and Null and isValid', () => { global.console.warn = jest.genMockFunction()// moment.js otherString will throw warn expect(dayjs('otherString').toString().toLowerCase()).toBe(moment('otherString').toString().toLowerCase()) expect(dayjs(undefined).toDate()).toEqual(moment(undefined).toDate()) expect(dayjs().isValid()).toBe(true) expect(dayjs(undefined).isValid()).toBe(true) expect(dayjs('').isValid()).toBe(false) expect(dayjs(null).isValid()).toBe(false) expect(dayjs('otherString').isValid()).toBe(false) expect(dayjs(null).toString().toLowerCase()).toBe(moment(null).toString().toLowerCase()) })})
it('Unix Timestamp Number (milliseconds) 1523520536000', () => { const timestamp = 1523520536000 expect(dayjs(timestamp).valueOf()).toBe(moment(timestamp).valueOf())})
it('Unix Timestamp Number (seconds) 1318781876', () => { const timestamp1 = 1318781876 const timestamp2 = 1318781876.721 expect(dayjs.unix(timestamp1).valueOf()).toBe(moment.unix(timestamp1).valueOf()) expect(dayjs.unix(timestamp2).valueOf()).toBe(moment.unix(timestamp2).valueOf())})
it('String and Number 20180101', () => { expect(dayjs(20180101).valueOf()).toBe(moment(20180101).valueOf()) expect(dayjs('20180101').valueOf()).toBe(moment('20180101').valueOf())})
it('Number 0', () => { expect(dayjs(0).valueOf()).toBe(moment(0).valueOf())})
it('Clone not affect each other', () => { const base = dayjs(20170101) const year = base.year() const another = base.set('year', year + 1) expect(another.unix() - base.unix()).toBe(31536000)})
it('Clone with same value', () => { const base = dayjs() const year = base.year() const newBase = base.set('year', year + 1) const another = newBase.clone() expect(newBase.toString()).toBe(another.toString())})
describe('REGEX_PARSE', () => { it('2020/9/30', () => { const date = '2020/9/30' const d = date.match(REGEX_PARSE) expect(dayjs(date).valueOf()).toBe(moment(date).valueOf()) expect(d.join('-')).toBe('2020/9/30-2020-9-30----') }) it('2019-03-25T06:41:00.999999999', () => { const date = '2019-03-25T06:41:00.999999999' const d = date.match(REGEX_PARSE) expect(dayjs(date).valueOf()).toBe(moment(date).valueOf()) expect(d.join('-')).toBe('2019-03-25T06:41:00.999999999-2019-03-25-06-41-00-999999999') }) it('20210102T012345', () => { const date = '20210102T012345' const d = date.match(REGEX_PARSE) expect(dayjs(date).valueOf()).toBe(moment(date).valueOf()) expect(d.join('-')).toBe('20210102T012345-2021-01-02-01-23-45-') }) it('2021-01-02T01:23', () => { const date = '2021-01-02T01:23' const d = date.match(REGEX_PARSE) expect(dayjs(date).valueOf()).toBe(moment(date).valueOf()) expect(d.join('-')).toBe('2021-01-02T01:23-2021-01-02-01-23--') }) it('2021-01-02T01:23:45', () => { const date = '2021-01-02T01:23:45' const d = date.match(REGEX_PARSE) expect(dayjs(date).valueOf()).toBe(moment(date).valueOf()) expect(d.join('-')).toBe('2021-01-02T01:23:45-2021-01-02-01-23-45-') })
it('2020-12-31T18:00:00.000-0500 (no regex match)', () => { const date = '2020-12-31T18:00:00.000-0500' const d = date.match(REGEX_PARSE) expect(dayjs(date).valueOf()).toBe(moment(date).valueOf()) expect(d).toBe(null) })
// format used in timezone plugin utcString it('2021-1-4 0:42:53:000', () => { const date = '2021-1-4 0:42:53:000' const d = date.match(REGEX_PARSE) expect(dayjs(date).valueOf()).toBe(moment(date).valueOf()) expect(d.join('-')).toBe('2021-1-4 0:42:53:000-2021-1-4-0-42-53-000') })
it('2020-12-31T18:00:00-05:00 (no regex match)', () => { const date = '2020-12-31T18:00:00-05:00' const d = date.match(REGEX_PARSE) expect(dayjs(date).valueOf()).toBe(moment(date).valueOf()) expect(d).toBe(null) })
it('2021-01-02T01:23:45-0500 (no regex match)', () => { const date = '2021-01-02T01:23:45-0500' const d = date.match(REGEX_PARSE) expect(dayjs(date).valueOf()).toBe(moment(date).valueOf()) expect(d).toBe(null) }) it('2021-01-02T01:23:45Z (no regex match)', () => { const date = '2021-01-02T01:23:45Z' const d = date.match(REGEX_PARSE) expect(dayjs(date).valueOf()).toBe(moment(date).valueOf()) expect(d).toBe(null) })
// dots should not be matched, and fallback to Date it('2021.01.03', () => { const date = '2021.01.03' const d = date.match(REGEX_PARSE) expect(dayjs(date).valueOf()).toBe(moment(date).valueOf()) expect(d).toBe(null) })})
dayjs

Version Info

Tagged at
a year ago