Skip to content

Commit

Permalink
fix: fix customParseFormat plugin strict mode parse meridiem bug
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun committed Jan 7, 2021
1 parent 344bdc0 commit 2d90abb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/plugin/customParseFormat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions test/plugin/customParseFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 2d90abb

Please sign in to comment.