Skip to content

Commit

Permalink
fix: fix isDayjs check logic (iamkun#2383)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun authored Jul 27, 2023
1 parent 8c7de64 commit 5f3f878
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ let L = 'en' // global locale
const Ls = {} // global loaded locale
Ls[L] = en

const isDayjs = d => d instanceof Dayjs // eslint-disable-line no-use-before-define
const IS_DAYJS = '$isDayjsObject'

// eslint-disable-next-line no-use-before-define
const isDayjs = d => d instanceof Dayjs || !!(d && d[IS_DAYJS])

const parseLocale = (preset, object, isLocal) => {
let l
Expand Down Expand Up @@ -72,7 +75,7 @@ const parseDate = (cfg) => {
|| 1, d[4] || 0, d[5] || 0, d[6] || 0, ms))
}
return new Date(d[1], m, d[3]
|| 1, d[4] || 0, d[5] || 0, d[6] || 0, ms)
|| 1, d[4] || 0, d[5] || 0, d[6] || 0, ms)
}
}

Expand All @@ -83,6 +86,7 @@ class Dayjs {
constructor(cfg) {
this.$L = parseLocale(cfg.locale, null, true)
this.parse(cfg) // for plugin
this[IS_DAYJS] = true
}

parse(cfg) {
Expand Down
8 changes: 8 additions & 0 deletions test/constructor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ it('supports instanceof dayjs', () => {
expect(dayjs() instanceof dayjs).toBeTruthy()
})

it('$isDayjsObject', () => {
const mockOtherVersionDayjsObj = {
$isDayjsObject: true
}
expect(dayjs.isDayjs(mockOtherVersionDayjsObj)).toBeTruthy()
})

it('does not break isDayjs', () => {
expect(dayjs.isDayjs(dayjs())).toBeTruthy()
expect(dayjs.isDayjs(new Date())).toBeFalsy()
})

0 comments on commit 5f3f878

Please sign in to comment.