Skip to content

Commit 41b1405

Browse files
authored
fix: customParseFormat plugin to parse comma as a separator character (#1913)
1 parent 05cba7e commit 41b1405

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
## [1.11.2](https://github.com/iamkun/dayjs/compare/v1.11.1...v1.11.2) (2022-05-06)
2+
3+
4+
### Bug Fixes
5+
6+
* add OpUnitType (week) to quarterOfYear startOf/endOf types ([#1865](https://github.com/iamkun/dayjs/issues/1865)) ([400bc3e](https://github.com/iamkun/dayjs/commit/400bc3e8915e0c58e7abbfd3a1235364b1abaf3e))
7+
* Fix type issue with ManipulateType ([#1864](https://github.com/iamkun/dayjs/issues/1864)) ([d033dfc](https://github.com/iamkun/dayjs/commit/d033dfcfc1d2ced39b2733898e8d85ad5984c9e9))
8+
* fix UTC plugin .valueOf not taking DST into account ([#1448](https://github.com/iamkun/dayjs/issues/1448)) ([27d1c50](https://github.com/iamkun/dayjs/commit/27d1c506100ae6624f258c21cc06b24768ced733))
9+
110
## [1.11.1](https://github.com/iamkun/dayjs/compare/v1.11.0...v1.11.1) (2022-04-15)
211

312

src/plugin/customParseFormat/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { u } from '../localizedFormat/utils'
22

3-
const formattingTokens = /(\[[^[]*\])|([-:/.()\s]+)|(A|a|YYYY|YY?|MM?M?M?|Do|DD?|hh?|HH?|mm?|ss?|S{1,3}|z|ZZ?)/g
3+
const formattingTokens = /(\[[^[]*\])|([-_:/.,()\s]+)|(A|a|YYYY|YY?|MM?M?M?|Do|DD?|hh?|HH?|mm?|ss?|S{1,3}|z|ZZ?)/g
44

55
const match1 = /\d/ // 0 - 9
66
const match2 = /\d\d/ // 00 - 99
@@ -9,7 +9,7 @@ const match4 = /\d{4}/ // 0000 - 9999
99
const match1to2 = /\d\d?/ // 0 - 99
1010
const matchSigned = /[+-]?\d+/ // -inf - inf
1111
const matchOffset = /[+-]\d\d:?(\d\d)?|Z/ // +00:00 -00:00 +0000 or -0000 +00 or Z
12-
const matchWord = /\d*[^\s\d-_:/()]+/ // Word
12+
const matchWord = /\d*[^-_:/,()\s\d]+/ // Word
1313

1414
let locale = {}
1515

test/plugin/customParseFormat.test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,61 @@ it('custom two-digit year parse function', () => {
370370
expect(dayjs(input3, format).year()).toBe(1899)
371371
})
372372

373+
// issue 1852
374+
describe('parse with special separator characters', () => {
375+
it('Output is NaN for a specific date format', () => {
376+
const input = '20 Nov, 2022'
377+
const format = 'DD MMM, YYYY'
378+
const locale = 'en'
379+
const resultDayjs = dayjs(input, format, locale)
380+
const resultMoment = moment(input, format, locale)
381+
expect(resultMoment.isValid()).toBe(true)
382+
expect(resultDayjs.isValid()).toBe(true)
383+
expect(resultDayjs.format('DD-MM-YYYY')).toBe('20-11-2022')
384+
expect(resultMoment.format('DD-MM-YYYY')).toBe('20-11-2022')
385+
})
386+
it('parse comma separated date', () => {
387+
const input = '20,11,2022'
388+
const format = 'DD,MM,YYYY'
389+
const resultDayjs = dayjs(input, format)
390+
const resultMoment = moment(input, format)
391+
expect(resultMoment.isValid()).toBe(true)
392+
expect(resultDayjs.isValid()).toBe(true)
393+
expect(resultDayjs.format('DD-MM-YYYY')).toBe('20-11-2022')
394+
expect(resultMoment.format('DD-MM-YYYY')).toBe('20-11-2022')
395+
})
396+
it('parse comma separated date in strict mode', () => {
397+
const input = '20,11,2022'
398+
const format = 'DD,MM,YYYY'
399+
const resultDayjs = dayjs(input, format, true)
400+
const resultMoment = moment(input, format, true)
401+
expect(resultMoment.isValid()).toBe(true)
402+
expect(resultDayjs.isValid()).toBe(true)
403+
expect(resultDayjs.format('DD-MM-YYYY')).toBe('20-11-2022')
404+
expect(resultMoment.format('DD-MM-YYYY')).toBe('20-11-2022')
405+
})
406+
it('parse date with multi character separator', () => {
407+
const input = '20---11---2022'
408+
const format = 'DD-/-MM-#-YYYY'
409+
const resultDayjs = dayjs(input, format)
410+
const resultMoment = moment(input, format)
411+
expect(resultMoment.isValid()).toBe(true)
412+
expect(resultDayjs.isValid()).toBe(true)
413+
expect(resultDayjs.format('DD-MM-YYYY')).toBe('20-11-2022')
414+
expect(resultMoment.format('DD-MM-YYYY')).toBe('20-11-2022')
415+
})
416+
it('parse date with multi character separator in strict mode', () => {
417+
const input = '20-/-11-#-2022'
418+
const format = 'DD-/-MM-#-YYYY'
419+
const resultDayjs = dayjs(input, format, true)
420+
const resultMoment = moment(input, format, true)
421+
expect(resultMoment.isValid()).toBe(true)
422+
expect(resultDayjs.isValid()).toBe(true)
423+
expect(resultDayjs.format('DD-MM-YYYY')).toBe('20-11-2022')
424+
expect(resultMoment.format('DD-MM-YYYY')).toBe('20-11-2022')
425+
})
426+
})
427+
373428
it('parse X x', () => {
374429
const input = '1410715640.579'
375430
const format = 'X'

0 commit comments

Comments
 (0)