Skip to content

Commit 97856c6

Browse files
authoredMay 14, 2020
fix: Update CustomParseFormat plugin to support Array formats (#906)
1 parent f355235 commit 97856c6

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed
 

‎src/plugin/customParseFormat/index.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,24 @@ export default (o, C, d) => {
196196
locale = pl ? d.Ls[pl] : this.$locale()
197197
}
198198
this.$d = parseFormattedInput(date, format, utc)
199-
this.init(cfg)
199+
this.init()
200+
if (pl && pl !== true) this.$L = this.locale(pl).$L
200201
if (isStrict && date !== this.format(format)) {
201202
this.$d = new Date('')
202203
}
203-
if (pl && pl !== true) this.$L = this.locale(pl).$L
204+
} else if (format instanceof Array) {
205+
const len = format.length
206+
for (let i = 1; i <= len; i += 1) {
207+
args[1] = format[i - 1]
208+
const result = d.apply(this, args)
209+
if (result.isValid()) {
210+
this.$d = result.$d
211+
this.$L = result.$L
212+
this.init()
213+
break
214+
}
215+
if (i === len) this.$d = new Date('')
216+
}
204217
} else {
205218
oldParse.call(this, cfg)
206219
}

‎test/plugin/customParseFormat.test.js

+19
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,22 @@ describe('Strict mode', () => {
248248
expect(dayjs(input, format, 'zh-cn', true).isValid()).toBe(false)
249249
})
250250
})
251+
252+
describe('Array format support', () => {
253+
it('second ok', () => {
254+
const input = '2012-05-28'
255+
const format = ['YYYY', 'YYYY-MM-DD']
256+
expect(dayjs(input, format).isValid()).toBe(true)
257+
expect(dayjs(input, format, true).format('YYYY-MM-DD')).toBe('2012-05-28')
258+
})
259+
it('all invalid', () => {
260+
const input = '2012-05-28'
261+
const format = ['DD', 'MM-DD']
262+
expect(dayjs(input, format, true).isValid()).toBe(false)
263+
})
264+
it('with locale', () => {
265+
const input = '2018 三月 12'
266+
const format = ['YYYY', 'MM', 'YYYY MMMM DD']
267+
expect(dayjs(input, format, 'zh-cn', true).format('YYYY MMMM DD')).toBe(input)
268+
})
269+
})

0 commit comments

Comments
 (0)
Please sign in to comment.