Skip to content

Commit 5f3f878

Browse files
authored
fix: fix isDayjs check logic (#2383)
1 parent 8c7de64 commit 5f3f878

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ let L = 'en' // global locale
66
const Ls = {} // global loaded locale
77
Ls[L] = en
88

9-
const isDayjs = d => d instanceof Dayjs // eslint-disable-line no-use-before-define
9+
const IS_DAYJS = '$isDayjsObject'
10+
11+
// eslint-disable-next-line no-use-before-define
12+
const isDayjs = d => d instanceof Dayjs || !!(d && d[IS_DAYJS])
1013

1114
const parseLocale = (preset, object, isLocal) => {
1215
let l
@@ -72,7 +75,7 @@ const parseDate = (cfg) => {
7275
|| 1, d[4] || 0, d[5] || 0, d[6] || 0, ms))
7376
}
7477
return new Date(d[1], m, d[3]
75-
|| 1, d[4] || 0, d[5] || 0, d[6] || 0, ms)
78+
|| 1, d[4] || 0, d[5] || 0, d[6] || 0, ms)
7679
}
7780
}
7881

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

8892
parse(cfg) {

test/constructor.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ it('supports instanceof dayjs', () => {
1313
expect(dayjs() instanceof dayjs).toBeTruthy()
1414
})
1515

16+
it('$isDayjsObject', () => {
17+
const mockOtherVersionDayjsObj = {
18+
$isDayjsObject: true
19+
}
20+
expect(dayjs.isDayjs(mockOtherVersionDayjsObj)).toBeTruthy()
21+
})
22+
1623
it('does not break isDayjs', () => {
1724
expect(dayjs.isDayjs(dayjs())).toBeTruthy()
25+
expect(dayjs.isDayjs(new Date())).toBeFalsy()
1826
})

0 commit comments

Comments
 (0)