From 94e47c2f823330c8589d5cf46d95b761c0ccfa84 Mon Sep 17 00:00:00 2001 From: iamkun Date: Tue, 13 Oct 2020 15:39:13 +0800 Subject: [PATCH 1/2] fix: add arraySupport plugin --- src/plugin/arraySupport/index.js | 27 +++++++++++++++++ test/plugin/arraySupport.test.js | 52 ++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 src/plugin/arraySupport/index.js create mode 100755 test/plugin/arraySupport.test.js diff --git a/src/plugin/arraySupport/index.js b/src/plugin/arraySupport/index.js new file mode 100644 index 000000000..5b0eb39f2 --- /dev/null +++ b/src/plugin/arraySupport/index.js @@ -0,0 +1,27 @@ +export default (o, c, dayjs) => { + const proto = c.prototype + const parseDate = (cfg) => { + const { date, utc } = cfg + if (Array.isArray(date)) { + const isEmptyArray = date.length === 0 + const isYearOnly = date.length === 1 + if (utc) { + if (isEmptyArray) { + return new Date() + } + return new Date(Date.UTC.apply(null, date)) + } + if (isYearOnly) { + return dayjs(String(date[0])).toDate() + } + return new (Function.prototype.bind.apply(Date, [null].concat(date)))() + } + return date + } + + const oldParse = proto.parse + proto.parse = function (cfg) { + cfg.date = parseDate.bind(this)(cfg) + oldParse.bind(this)(cfg) + } +} diff --git a/test/plugin/arraySupport.test.js b/test/plugin/arraySupport.test.js new file mode 100755 index 000000000..dc5dda7ad --- /dev/null +++ b/test/plugin/arraySupport.test.js @@ -0,0 +1,52 @@ +import MockDate from 'mockdate' +import moment from 'moment' +import dayjs from '../../src' +import arraySupport from '../../src/plugin/arraySupport' +import utc from '../../src/plugin/utc' + +dayjs.extend(utc) +dayjs.extend(arraySupport) + +beforeEach(() => { + MockDate.set(new Date()) +}) + +afterEach(() => { + MockDate.reset() +}) + +describe('parse empty array', () => { + it('local', () => { + expect(dayjs([]).format()) + .toBe(moment([]).format()) + }) + it('utc', () => { + expect(dayjs.utc([]).format()) + .toBe(moment.utc([]).format()) + }) +}) + +const testArrs = [ + [2010, 1, 14, 15, 25, 50, 125], + [2010], + [2010, 6], + [2010, 6, 10] +] + +describe('parse array local', () => { + testArrs.forEach((testArr) => { + it(testArr, () => { + expect(dayjs(testArr).format()) + .toBe(moment(testArr).format()) + }) + }) +}) + +describe('parse array utc', () => { + testArrs.forEach((testArr) => { + it(testArr, () => { + expect(dayjs.utc(testArr).format()) + .toBe(moment.utc(testArr).format()) + }) + }) +}) From d3ddc3aa5e22c0d8d1ee95c220ae7189f4976737 Mon Sep 17 00:00:00 2001 From: iamkun Date: Tue, 13 Oct 2020 16:01:30 +0800 Subject: [PATCH 2/2] chore: update --- src/plugin/arraySupport/index.js | 6 ++---- types/plugin/arraySupport.d.ts | 4 ++++ 2 files changed, 6 insertions(+), 4 deletions(-) create mode 100755 types/plugin/arraySupport.d.ts diff --git a/src/plugin/arraySupport/index.js b/src/plugin/arraySupport/index.js index 5b0eb39f2..df43727ff 100644 --- a/src/plugin/arraySupport/index.js +++ b/src/plugin/arraySupport/index.js @@ -3,15 +3,13 @@ export default (o, c, dayjs) => { const parseDate = (cfg) => { const { date, utc } = cfg if (Array.isArray(date)) { - const isEmptyArray = date.length === 0 - const isYearOnly = date.length === 1 if (utc) { - if (isEmptyArray) { + if (!date.length) { return new Date() } return new Date(Date.UTC.apply(null, date)) } - if (isYearOnly) { + if (date.length === 1) { return dayjs(String(date[0])).toDate() } return new (Function.prototype.bind.apply(Date, [null].concat(date)))() diff --git a/types/plugin/arraySupport.d.ts b/types/plugin/arraySupport.d.ts new file mode 100755 index 000000000..30ec75e5d --- /dev/null +++ b/types/plugin/arraySupport.d.ts @@ -0,0 +1,4 @@ +import { PluginFunc } from 'dayjs' + +declare const plugin: PluginFunc +export = plugin