Skip to content

Commit

Permalink
refactor: update customParseFormat, allow parse locale from argument
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun committed Feb 23, 2019
1 parent bbc2ed7 commit a0a97ff
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ const parseLocale = (preset, object, isLocal) => {
return l
}

const dayjs = (date, c) => {
const dayjs = (date, c, pl) => {
if (isDayjs(date)) {
return date.clone()
}
// eslint-disable-next-line no-nested-ternary
const cfg = c ? (typeof c === 'string' ? { format: c } : c) : {}
const cfg = c ? (typeof c === 'string' ? { format: c, pl } : c) : {}
cfg.date = date
return new Dayjs(cfg) // eslint-disable-line no-use-before-define
}
Expand Down Expand Up @@ -408,5 +408,6 @@ dayjs.unix = timestamp => (
)

dayjs.en = Ls[L]
dayjs.Ls = Ls

export default dayjs
6 changes: 3 additions & 3 deletions src/plugin/customParseFormat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,13 @@ const parseFormattedInput = (input, format) => {
}


export default (o, C) => {
export default (o, C, d) => {
const proto = C.prototype
const oldParse = proto.parse
proto.parse = function (cfg) {
const { date: input, format } = cfg
const { date: input, format, pl } = cfg
if (format) {
locale = this.$locale()
locale = pl ? d.Ls[pl] : this.$locale()
this.$d = parseFormattedInput(input, format)
this.init(cfg)
} else {
Expand Down
13 changes: 13 additions & 0 deletions test/plugin/customParseFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ it('parse month from short string with locale in config', () => {
expect(dayjs(input, { format, locale: uk }).valueOf()).toBe(moment(input, format, 'uk').valueOf())
})

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

it('parse month from string with locale in argument', () => {
const input = '2018 лютий 03'
const format = 'YYYY MMMM DD'

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

it('return Invalid Date when parse corrupt string', () => {
const input = '2018 Turnip 03'
const format = 'YYYY MMMM DD'
Expand Down

0 comments on commit a0a97ff

Please sign in to comment.