From 2d90abbd610a8246b0c2a7791c155c7e2b524b9f Mon Sep 17 00:00:00 2001 From: iamkun Date: Thu, 7 Jan 2021 20:22:55 +0800 Subject: [PATCH] fix: fix customParseFormat plugin strict mode parse meridiem bug --- src/plugin/customParseFormat/index.js | 9 +++++---- test/plugin/customParseFormat.test.js | 2 ++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/plugin/customParseFormat/index.js b/src/plugin/customParseFormat/index.js index f2eabb2b7..12c7803e8 100644 --- a/src/plugin/customParseFormat/index.js +++ b/src/plugin/customParseFormat/index.js @@ -11,7 +11,7 @@ const matchSigned = /[+-]?\d+/ // -inf - inf const matchOffset = /[+-]\d\d:?(\d\d)?/ // +00:00 -00:00 +0000 or -0000 +00 const matchWord = /\d*[^\s\d-:/()]+/ // Word -let locale +let locale = {} function offsetFromString(string) { if (!string) return 0 @@ -216,8 +216,9 @@ export default (o, C, d) => { const isStrict = isStrictWithoutLocale || isStrictWithLocale let pl = args[2] if (isStrictWithLocale) [,, pl] = args - if (!isStrictWithoutLocale) { - locale = pl ? d.Ls[pl] : this.$locale() + locale = this.$locale() + if (!isStrictWithoutLocale && pl) { + locale = d.Ls[pl] } this.$d = parseFormattedInput(date, format, utc) this.init() @@ -226,7 +227,7 @@ export default (o, C, d) => { this.$d = new Date('') } // reset global locale to make parallel unit test - locale = undefined + locale = {} } else if (format instanceof Array) { const len = format.length for (let i = 1; i <= len; i += 1) { diff --git a/test/plugin/customParseFormat.test.js b/test/plugin/customParseFormat.test.js index ef8bfd625..bc78e74b8 100644 --- a/test/plugin/customParseFormat.test.js +++ b/test/plugin/customParseFormat.test.js @@ -294,6 +294,8 @@ describe('Strict mode', () => { const format = 'YYYY-MM-DD' expect(dayjs(input, format).isValid()).toBe(true) expect(dayjs(input, format, true).isValid()).toBe(false) + expect(dayjs('2020-Jan-01', 'YYYY-MMM-DD', true).isValid()).toBe(true) + expect(dayjs('30/1/2020 10:59 PM', 'D/M/YYYY h:mm A', true).isValid()).toBe(true) }) it('with locale', () => { const input = '2018 三月 99'