Skip to content

Commit 877b1c1

Browse files
committed
fix: fix regex parse bug
1 parent 7dbdd1d commit 877b1c1

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

src/constant.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ export const MONTHS = 'January.February.March.April.May.June.July.August.Septemb
2626
export const FORMAT_DEFAULT = 'YYYY-MM-DDTHH:mm:ssZ'
2727

2828
// regex
29-
export const REGEX_PARSE = /^(\d{4})-?(\d{2})-?(\d{1,2})(.*(\d{2}):(\d{2}):(\d{2}))?.?(\d{3})?$/
29+
export const REGEX_PARSE = /^(\d{4})-?(\d{1,2})-?(\d{1,2})(.*?(\d{1,2}):(\d{1,2}):(\d{1,2}))?.?(\d{1,3})?$/
3030
export const REGEX_FORMAT = /\[.*?\]|Y{2,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g

test/parse.test.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@ describe('Parse', () => {
1717

1818
it('String 20130208', () => {
1919
global.console.warn = jest.genMockFunction()// moment.js '2018-4-1 1:1:1:22' will throw warn
20-
const timeArr = [
21-
'20130108',
22-
'2018-04-24',
23-
'2018-05-02 11:12:13',
24-
'2018-05-02 11:12:13.998',
25-
'2018-4-1', // not recommend
26-
'2018-4-1 1:1:1:22' // not recommend
27-
]
28-
timeArr.forEach((t) => {
29-
expect(dayjs(t).valueOf()).toBe(moment(t).valueOf())
30-
})
20+
let d = '20130108'
21+
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf())
22+
d = '2018-04-24'
23+
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf())
24+
d = '2018-05-02 11:12:13'
25+
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf())
26+
d = '2018-05-02 11:12:13.998'
27+
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf())
28+
d = '2018-4-1'
29+
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf()) // not recommend
30+
d = '2018-4-1 1:1:1:223'
31+
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf()) // not recommend
3132
})
3233

3334
it('String ISO 8601 date, time and zone', () => {

0 commit comments

Comments
 (0)