Skip to content

Commit

Permalink
fix: Update CustomParseFormat plugin to support Array formats (#906)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun committed May 14, 2020
1 parent f355235 commit 97856c6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/plugin/customParseFormat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,24 @@ export default (o, C, d) => {
locale = pl ? d.Ls[pl] : this.$locale()
}
this.$d = parseFormattedInput(date, format, utc)
this.init(cfg)
this.init()
if (pl && pl !== true) this.$L = this.locale(pl).$L
if (isStrict && date !== this.format(format)) {
this.$d = new Date('')
}
if (pl && pl !== true) this.$L = this.locale(pl).$L
} else if (format instanceof Array) {
const len = format.length
for (let i = 1; i <= len; i += 1) {
args[1] = format[i - 1]
const result = d.apply(this, args)
if (result.isValid()) {
this.$d = result.$d
this.$L = result.$L
this.init()
break
}
if (i === len) this.$d = new Date('')
}
} else {
oldParse.call(this, cfg)
}
Expand Down
19 changes: 19 additions & 0 deletions test/plugin/customParseFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,22 @@ describe('Strict mode', () => {
expect(dayjs(input, format, 'zh-cn', true).isValid()).toBe(false)
})
})

describe('Array format support', () => {
it('second ok', () => {
const input = '2012-05-28'
const format = ['YYYY', 'YYYY-MM-DD']
expect(dayjs(input, format).isValid()).toBe(true)
expect(dayjs(input, format, true).format('YYYY-MM-DD')).toBe('2012-05-28')
})
it('all invalid', () => {
const input = '2012-05-28'
const format = ['DD', 'MM-DD']
expect(dayjs(input, format, true).isValid()).toBe(false)
})
it('with locale', () => {
const input = '2018 三月 12'
const format = ['YYYY', 'MM', 'YYYY MMMM DD']
expect(dayjs(input, format, 'zh-cn', true).format('YYYY MMMM DD')).toBe(input)
})
})

0 comments on commit 97856c6

Please sign in to comment.