Repository: iamkun/dayjs
Pull request #1928 (2022-06-06 02:46:58)

D2M


Patch:
diff --git a/src/locale/nl.js b/src/locale/nl.js
index b0f15a7..6d9ff7b 100644
--- a/src/locale/nl.js
+++ b/src/locale/nl.js
@@ -8,7 +8,7 @@ const locale = {
   weekdaysMin: 'zo_ma_di_wo_do_vr_za'.split('_'),
   months: 'januari_februari_maart_april_mei_juni_juli_augustus_september_oktober_november_december'.split('_'),
   monthsShort: 'jan_feb_mrt_apr_mei_jun_jul_aug_sep_okt_nov_dec'.split('_'),
-  ordinal: n => `${n}.`,
+  ordinal: n => `${n}${n === 1 || n === 8 || n >= 20 ? 'ste' : 'de'}`,
   weekStart: 1,
   yearStart: 4,
   formats: {
diff --git a/src/plugin/customParseFormat/index.js b/src/plugin/customParseFormat/index.js
index f662a4a..771d131 100644
--- a/src/plugin/customParseFormat/index.js
+++ b/src/plugin/customParseFormat/index.js
@@ -1,6 +1,6 @@
 import { u } from '../localizedFormat/utils'
 
-const formattingTokens = /(\[[^[]*\])|([-:/.()\s]+)|(A|a|YYYY|YY?|MM?M?M?|Do|DD?|hh?|HH?|mm?|ss?|S{1,3}|z|ZZ?)/g
+const formattingTokens = /(\[[^[]*\])|([-_:/.,()\s]+)|(A|a|YYYY|YY?|MM?M?M?|Do|DD?|hh?|HH?|mm?|ss?|S{1,3}|z|ZZ?)/g
 
 const match1 = /\d/ // 0 - 9
 const match2 = /\d\d/ // 00 - 99
@@ -9,7 +9,7 @@ const match4 = /\d{4}/ // 0000 - 9999
 const match1to2 = /\d\d?/ // 0 - 99
 const matchSigned = /[+-]?\d+/ // -inf - inf
 const matchOffset = /[+-]\d\d:?(\d\d)?|Z/ // +00:00 -00:00 +0000 or -0000 +00 or Z
-const matchWord = /\d*[^\s\d-_:/()]+/ // Word
+const matchWord = /\d*[^-_:/,()\s\d]+/ // Word
 
 let locale = {}
 


Test patch:
diff --git a/test/plugin/customParseFormat.test.js b/test/plugin/customParseFormat.test.js
index 3b4e5f9..a4588e9 100644
--- a/test/plugin/customParseFormat.test.js
+++ b/test/plugin/customParseFormat.test.js
@@ -370,6 +370,61 @@ it('custom two-digit year parse function', () => {
   expect(dayjs(input3, format).year()).toBe(1899)
 })
 
+// issue 1852
+describe('parse with special separator characters', () => {
+  it('Output is NaN for a specific date format', () => {
+    const input = '20 Nov, 2022'
+    const format = 'DD MMM, YYYY'
+    const locale = 'en'
+    const resultDayjs = dayjs(input, format, locale)
+    const resultMoment = moment(input, format, locale)
+    expect(resultMoment.isValid()).toBe(true)
+    expect(resultDayjs.isValid()).toBe(true)
+    expect(resultDayjs.format('DD-MM-YYYY')).toBe('20-11-2022')
+    expect(resultMoment.format('DD-MM-YYYY')).toBe('20-11-2022')
+  })
+  it('parse comma separated date', () => {
+    const input = '20,11,2022'
+    const format = 'DD,MM,YYYY'
+    const resultDayjs = dayjs(input, format)
+    const resultMoment = moment(input, format)
+    expect(resultMoment.isValid()).toBe(true)
+    expect(resultDayjs.isValid()).toBe(true)
+    expect(resultDayjs.format('DD-MM-YYYY')).toBe('20-11-2022')
+    expect(resultMoment.format('DD-MM-YYYY')).toBe('20-11-2022')
+  })
+  it('parse comma separated date in strict mode', () => {
+    const input = '20,11,2022'
+    const format = 'DD,MM,YYYY'
+    const resultDayjs = dayjs(input, format, true)
+    const resultMoment = moment(input, format, true)
+    expect(resultMoment.isValid()).toBe(true)
+    expect(resultDayjs.isValid()).toBe(true)
+    expect(resultDayjs.format('DD-MM-YYYY')).toBe('20-11-2022')
+    expect(resultMoment.format('DD-MM-YYYY')).toBe('20-11-2022')
+  })
+  it('parse date with multi character separator', () => {
+    const input = '20---11---2022'
+    const format = 'DD-/-MM-#-YYYY'
+    const resultDayjs = dayjs(input, format)
+    const resultMoment = moment(input, format)
+    expect(resultMoment.isValid()).toBe(true)
+    expect(resultDayjs.isValid()).toBe(true)
+    expect(resultDayjs.format('DD-MM-YYYY')).toBe('20-11-2022')
+    expect(resultMoment.format('DD-MM-YYYY')).toBe('20-11-2022')
+  })
+  it('parse date with multi character separator in strict mode', () => {
+    const input = '20-/-11-#-2022'
+    const format = 'DD-/-MM-#-YYYY'
+    const resultDayjs = dayjs(input, format, true)
+    const resultMoment = moment(input, format, true)
+    expect(resultMoment.isValid()).toBe(true)
+    expect(resultDayjs.isValid()).toBe(true)
+    expect(resultDayjs.format('DD-MM-YYYY')).toBe('20-11-2022')
+    expect(resultMoment.format('DD-MM-YYYY')).toBe('20-11-2022')
+  })
+})
+
 it('parse X x', () => {
   const input = '1410715640.579'
   const format = 'X'