Skip to content

Commit

Permalink
fix: Update CustomParseFormat plugin strict mode (#882)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun authored Apr 27, 2020
1 parent 2977438 commit db642ac
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ const parseLocale = (preset, object, isLocal) => {
return l || (!isLocal && L)
}

const dayjs = (date, c, pl) => {
const dayjs = function (date, c) {
if (isDayjs(date)) {
return date.clone()
}
// eslint-disable-next-line no-nested-ternary
const cfg = c ? (typeof c === 'string' ? { format: c, pl } : c) : {}
const cfg = typeof c === 'object' ? c : {}
cfg.date = date
cfg.args = arguments// eslint-disable-line prefer-rest-params
return new Dayjs(cfg) // eslint-disable-line no-use-before-define
}

Expand Down
22 changes: 16 additions & 6 deletions src/plugin/customParseFormat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,26 @@ export default (o, C, d) => {
proto.parse = function (cfg) {
const {
date,
format,
pl,
utc
utc,
args
} = cfg
this.$u = utc
if (format) {
locale = pl ? d.Ls[pl] : this.$locale()
const format = args[1]
if (typeof format === 'string') {
const isStrictWithoutLocale = args[2] === true
const isStrictWithLocale = args[3] === true
const isStrict = isStrictWithoutLocale || isStrictWithLocale
let pl = args[2]
if (isStrictWithLocale) [,, pl] = args
if (!isStrictWithoutLocale) {
locale = pl ? d.Ls[pl] : this.$locale()
}
this.$d = parseFormattedInput(date, format, utc)
this.init(cfg)
if (pl) this.$L = pl
if (isStrict && date !== this.format(format)) {
this.$d = new Date('')
}
if (pl && pl !== true) this.$L = pl
} else {
oldParse.call(this, cfg)
}
Expand Down
4 changes: 2 additions & 2 deletions src/plugin/utc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { MILLISECONDS_A_MINUTE, MIN } from '../../constant'
export default (option, Dayjs, dayjs) => {
const localOffset = (new Date()).getTimezoneOffset()
const proto = Dayjs.prototype
dayjs.utc = function (date, format) {
const cfg = { date, utc: true, format }
dayjs.utc = function (date) {
const cfg = { date, utc: true, args: arguments } // eslint-disable-line prefer-rest-params
return new Dayjs(cfg) // eslint-disable-line no-use-before-define
}

Expand Down
20 changes: 18 additions & 2 deletions test/plugin/customParseFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,13 @@ it('parse month from string with locale in config', () => {
const input = '2018 лютий 03'
const format = 'YYYY MMMM DD'

expect(dayjs(input, { format, locale: uk }).valueOf()).toBe(moment(input, format, 'uk').valueOf())
expect(dayjs(input, format, 'uk').valueOf()).toBe(moment(input, format, 'uk').valueOf())
})

it('parse month from short string with locale in config', () => {
const input = '2018 трав 03'
const format = 'YYYY MMM DD'
expect(dayjs(input, { format, locale: uk }).valueOf()).toBe(moment(input, format, 'uk').valueOf())
expect(dayjs(input, format, 'uk').valueOf()).toBe(moment(input, format, 'uk').valueOf())
})

it('parse month from short string with locale in argument', () => {
Expand Down Expand Up @@ -232,3 +232,19 @@ it('correctly parse ordinal', () => {
expect(dayjsCN.locale())
.toBe(momentCN.locale())
})


describe('Strict mode', () => {
it('without locale', () => {
const input = '1970-00-00'
const format = 'YYYY-MM-DD'
expect(dayjs(input, format).isValid()).toBe(true)
expect(dayjs(input, format, true).isValid()).toBe(false)
})
it('with locale', () => {
const input = '2018 三月 99'
const format = 'YYYY MMMM DD'
expect(dayjs(input, format, 'zh-cn').isValid()).toBe(true)
expect(dayjs(input, format, 'zh-cn', true).isValid()).toBe(false)
})
})
1 change: 1 addition & 0 deletions test/plugin/isoWeek.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ it('isoWeekday', () => {
for (let i = 0; i < 7; i += 1) {
expect(dayjs().add(i, 'day').isoWeekday()).toBe(moment().add(i, 'day').isoWeekday())
expect(dayjs().isoWeekday(i).valueOf()).toBe(moment().isoWeekday(i).valueOf())
expect(dayjs().add(1, 'day').isoWeekday(i).valueOf()).toBe(moment().add(1, 'day').isoWeekday(i).valueOf())
}
})

Expand Down

0 comments on commit db642ac

Please sign in to comment.