From f56783e14d8cf50916b015e7188b23bb6fbca839 Mon Sep 17 00:00:00 2001 From: iamkun Date: Tue, 13 Oct 2020 16:05:06 +0800 Subject: [PATCH] fix: Update objectSupport plugin to return current date time while parsing empty object --- src/plugin/objectSupport/index.js | 3 +++ test/plugin/objectSupport.test.js | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/plugin/objectSupport/index.js b/src/plugin/objectSupport/index.js index 33b5a62da..4344dc402 100644 --- a/src/plugin/objectSupport/index.js +++ b/src/plugin/objectSupport/index.js @@ -9,6 +9,9 @@ export default (o, c, dayjs) => { const { date, utc } = cfg const $d = {} if (isObject(date)) { + if (!Object.keys(date).length) { + return new Date() + } const now = utc ? dayjs.utc() : dayjs() Object.keys(date).forEach((k) => { $d[prettyUnit(k)] = date[k] diff --git a/test/plugin/objectSupport.test.js b/test/plugin/objectSupport.test.js index 392bdbf7c..06b10f9dc 100755 --- a/test/plugin/objectSupport.test.js +++ b/test/plugin/objectSupport.test.js @@ -114,6 +114,18 @@ const tests = [ '2010-02-14 15:25:50.125' ] ] + +describe('parse empty object', () => { + it('local', () => { + expect(dayjs({}).format()) + .toBe(moment({}).format()) + }) + it('utc', () => { + expect(dayjs.utc({}).format()) + .toBe(moment.utc({}).format()) + }) +}) + it('Constructor from Object', () => { for (let i = 0; i < tests.length; i += 1) { expect(dayjs(tests[i][0]).format(fmt)).toBe(tests[i][1])