Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update CustomParseFormat plugin to support Array formats #906

Merged
merged 2 commits into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})