From 1210e8639742c2ee8b6a328b726ea2bbffa912d2 Mon Sep 17 00:00:00 2001 From: Alexander Ulaev Date: Wed, 14 Jun 2023 19:30:23 +0400 Subject: [PATCH] fix: dayjs(null) throws error, not return dayjs object as invalid date --- src/plugin/objectSupport/index.js | 2 +- test/plugin/objectSupport.test.js | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/plugin/objectSupport/index.js b/src/plugin/objectSupport/index.js index 650166606..bd32358e9 100644 --- a/src/plugin/objectSupport/index.js +++ b/src/plugin/objectSupport/index.js @@ -1,6 +1,6 @@ export default (o, c, dayjs) => { const proto = c.prototype - const isObject = obj => !(obj instanceof Date) && !(obj instanceof Array) + 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) 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))