From 5f3f8786c796cd432fe6bcb6966a810daea89203 Mon Sep 17 00:00:00 2001 From: iamkun Date: Thu, 27 Jul 2023 14:08:45 +0800 Subject: [PATCH] fix: fix isDayjs check logic (#2383) --- src/index.js | 8 ++++++-- test/constructor.test.js | 8 ++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 16c2e351a..d26a401a6 100644 --- a/src/index.js +++ b/src/index.js @@ -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 @@ -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) } } @@ -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) { diff --git a/test/constructor.test.js b/test/constructor.test.js index b14b2fec4..66cdbfc68 100644 --- a/test/constructor.test.js +++ b/test/constructor.test.js @@ -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() })