From 858554ac99d765e242f436369b93f599a9a32331 Mon Sep 17 00:00:00 2001 From: alexander-ulaev <73880339+alexander-ulaev@users.noreply.github.com> Date: Thu, 22 Jun 2023 13:21:06 +0400 Subject: [PATCH] fix: dayjs(null) throws error, not return dayjs object as invalid date (#2334) Co-authored-by: iamkun --- src/plugin/objectSupport/index.js | 4 ++-- test/plugin/objectSupport.test.js | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/plugin/objectSupport/index.js b/src/plugin/objectSupport/index.js index 4800b6873..bd32358e9 100644 --- a/src/plugin/objectSupport/index.js +++ b/src/plugin/objectSupport/index.js @@ -1,7 +1,7 @@ export default (o, c, dayjs) => { const proto = c.prototype - const isObject = obj => !(obj instanceof Date) && !(obj instanceof Array) - && !proto.$utils().u(obj) && obj !== null && (obj.constructor.name === 'Object') + const isObject = obj => obj !== null && !(obj instanceof Date) && !(obj instanceof Array) + && !proto.$utils().u(obj) && (obj.constructor.name === 'Object') const prettyUnit = (u) => { const unit = proto.$utils().p(u) return unit === 'date' ? 'day' : unit diff --git a/test/plugin/objectSupport.test.js b/test/plugin/objectSupport.test.js index 06b10f9dc..68dd4e4af 100755 --- a/test/plugin/objectSupport.test.js +++ b/test/plugin/objectSupport.test.js @@ -140,6 +140,12 @@ it('Constructor from Object UTC', () => { expect(moment.utc(tests[i][0]).format(fmt)).toBe(result) } }) + +it('Constructor from null should return Invalid Date', () => { + expect(dayjs(null).isValid()).toBe(false) + expect(moment(null).isValid()).toBe(false) +}) + it('Set from Object', () => { for (let i = 0; i < tests.length; i += 1) { expect(dayjs(now).set(tests[i][0]).format(fmt)).toBe(moment(now).set(tests[i][0]).format(fmt))