From 954db86b8a0cba275a86e9d7b4f26239e68d3e45 Mon Sep 17 00:00:00 2001 From: Zero Date: Mon, 27 Apr 2020 20:27:02 +0800 Subject: [PATCH 01/14] feat add plugin objectSupport add/set/subtract support Object parameter and constructor support Object parameter --- src/plugin/objectSupport/index.js | 93 ++++++++ test/plugin/objectSupport.test.js | 339 ++++++++++++++++++++++++++++++ types/plugin/objectSupport.d.ts | 15 ++ 3 files changed, 447 insertions(+) create mode 100755 src/plugin/objectSupport/index.js create mode 100755 test/plugin/objectSupport.test.js create mode 100755 types/plugin/objectSupport.d.ts diff --git a/src/plugin/objectSupport/index.js b/src/plugin/objectSupport/index.js new file mode 100755 index 000000000..66b24b424 --- /dev/null +++ b/src/plugin/objectSupport/index.js @@ -0,0 +1,93 @@ +export default (o, c) => { + const proto = c.prototype + + /** + * Array to date + * @param d + * @param utc + * @returns {Date} + */ + const parseArrayArgument = (d, utc) => { + if (utc) { + return new Date(Date.UTC(d[1], d[2] - 1, d[3] + || 1, d[4] || 0, d[5] || 0, d[6] || 0, d[7] || 0)) + } + return new Date(d[1], d[2] - 1, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, d[7] || 0) + } + + /** + * Converts the object to a date array + * @param d Object { years, months, date, hours, minutes, seconds, milliseconds} + * @param utc + */ + const parseObjectArgument = (d, utc) => parseArrayArgument([ + 0, + d.y || d.year || d.years || 1970, + d.M || d.month || d.months || 1, + d.d || d.day || d.days || d.date || 1, + d.h || d.hour || d.hours || 0, + d.m || d.minute || d.minutes || 0, + d.s || d.second || d.seconds || 0, + d.ms || d.millisecond || d.milliseconds || 0 + ], utc) + + const isObject = obj => !(obj instanceof Date) && !(obj instanceof Array) && obj instanceof Object + const parseDate = (cfg) => { + const { date, utc } = cfg + if (isObject(date)) return parseObjectArgument(date, utc) + return date + } + + const oldParse = proto.parse + proto.parse = function (cfg) { + cfg.date = parseDate(cfg) + oldParse.bind(this)(cfg) + } + + proto.setObject = function (argument) { + const keys = Object.keys(argument) + let chain = this.clone() + keys.forEach((key) => { + chain = chain.$set(key, argument[key]) + }) + return chain + } + const oldSet = proto.set + proto.set = function (string, int) { + if (string instanceof Object) { + return this.setObject(string) + } + return oldSet.bind(this)(string, int) + } + + proto.addObject = function (argument) { + const keys = Object.keys(argument) + let chain = this + keys.forEach((key) => { + chain = chain.add(argument[key], key) + }) + return chain + } + const oldAdd = proto.add + proto.add = function (number, string) { + if (number instanceof Object) { + return this.addObject(number) + } + return oldAdd.bind(this)(number, string) + } + proto.subtractObject = function (argument) { + const keys = Object.keys(argument) + let chain = this + keys.forEach((key) => { + chain = chain.subtract(argument[key], key) + }) + return chain + } + const oldSubtract = proto.subtract + proto.subtract = function (number, string) { + if (number instanceof Object) { + return this.subtractObject(number) + } + return oldSubtract.bind(this)(number, string) + } +} diff --git a/test/plugin/objectSupport.test.js b/test/plugin/objectSupport.test.js new file mode 100755 index 000000000..15ad01675 --- /dev/null +++ b/test/plugin/objectSupport.test.js @@ -0,0 +1,339 @@ +import moment from 'moment' +import MockDate from 'mockdate' +import dayjs from '../../src' +import objectSupport from '../../src/plugin/objectSupport' +import quarterOfYear from '../../src/plugin/quarterOfYear' +import utc from '../../src/plugin/utc' + +dayjs.extend(utc) +dayjs.extend(quarterOfYear) +dayjs.extend(objectSupport) + +beforeEach(() => { + MockDate.set(new Date()) +}) + +afterEach(() => { + MockDate.reset() +}) +const now = new Date() +const fmt = 'YYYY-MM-DD HH:mm:ss.SSS' +const tests = [ + [{ year: 2010 }, '2010-01-01 00:00:00.000'], + [{ year: 2010, month: 1 }, '2010-01-01 00:00:00.000'], + [{ year: 2010, month: 1, day: 12 }, '2010-01-12 00:00:00.000'], + [{ year: 2010, month: 1, date: 12 }, '2010-01-12 00:00:00.000'], + [ + { + year: 2010, month: 1, day: 12, hours: 1 + }, + '2010-01-12 01:00:00.000' + ], + [ + { + year: 2010, month: 1, date: 12, hours: 1 + }, + '2010-01-12 01:00:00.000' + ], + [ + { + year: 2010, month: 1, day: 12, hours: 1, minutes: 1 + }, + '2010-01-12 01:01:00.000' + ], + [ + { + year: 2010, month: 1, date: 12, hours: 1, minutes: 1 + }, + '2010-01-12 01:01:00.000' + ], + [ + { + year: 2010, + month: 1, + day: 12, + hours: 1, + minutes: 1, + seconds: 1 + }, + '2010-01-12 01:01:01.000' + ], + [ + { + year: 2010, + month: 1, + day: 12, + hours: 1, + minutes: 1, + seconds: 1, + milliseconds: 1 + }, + '2010-01-12 01:01:01.001' + ], + [ + { + years: 2010, + months: 1, + days: 14, + hours: 15, + minutes: 25, + seconds: 50, + milliseconds: 125 + }, + '2010-01-14 15:25:50.125' + ], + [ + { + year: 2010, + month: 1, + day: 14, + hour: 15, + minute: 25, + second: 50, + millisecond: 125 + }, + '2010-01-14 15:25:50.125' + ], + [ + { + y: 2010, M: 1, d: 14, h: 15, m: 25, s: 50, ms: 125 + }, + '2010-01-14 15:25:50.125' + ] +] +it('Constructor from Object', () => { + for (let i = 0; i < tests.length; i += 1) { + expect(dayjs(tests[i][0]).format(fmt)).toBe(tests[i][1]) + } +}) + +it('Constructor from Object UTC', () => { + for (let i = 0; i < tests.length; i += 1) { + expect(dayjs.utc(tests[i][0]).format(fmt)).toBe(tests[i][1]) + } +}) +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)) + } +}) + +it('add short reverse args', () => { + const a = dayjs({ + year: 2011, + month: 9, + date: 12, + hour: 6, + minute: 7, + second: 8, + millisecond: 500 + }) + expect(a.add({ ms: 50 }).millisecond()).toBe(550) + expect(a.add({ s: 1 }).second()).toBe(9) + expect(a.add({ m: 1 }).minute()).toBe(8) + expect(a.add({ h: 1 }).hour()).toBe(7) + expect(a.add({ d: 1 }).date()).toBe(13) + expect(a.add({ w: 1 }).date()).toBe(19) + expect(a.add({ M: 1 }).month()).toBe(9) + expect(a.add({ y: 1 }).year()).toBe(2012) + expect(a.add({ Q: 1 }).month()).toBe(11) + + const b = dayjs([2010, 1, 31]).add({ M: 1 }) + const c = dayjs([2010, 2, 28]).subtract({ M: 1 }) + const d = dayjs([2010, 2, 28]).subtract({ Q: 1 }) + + expect(b.month()).toBe(1) + expect(b.date()).toBe(28) + expect(c.month()).toBe(0) + expect(c.date()).toBe(28) + expect(d.month()).toBe(10) + expect(d.date()).toBe(28) + expect(d.year()).toBe(2009) +}) + +it('add long reverse args', () => { + const a = dayjs({ + year: 2011, + month: 9, + date: 12, + hour: 6, + minute: 7, + second: 8, + millisecond: 500 + }) + + expect(a.add({ milliseconds: 50 }).millisecond()).toBe(550) + expect(a.add({ seconds: 1 }).second()).toBe(9) + expect(a.add({ minutes: 1 }).minute()).toBe(8) + expect(a.add({ hours: 1 }).hour()).toBe(7) + expect(a.add({ days: 1 }).date()).toBe(13) + expect(a.add({ weeks: 1 }).date()).toBe(19) + expect(a.add({ months: 1 }).month()).toBe(9) + expect(a.add({ years: 1 }).year()).toBe(2012) + expect(a.add({ quarters: 1 }).month()).toBe(11) +}) + +it('add long singular reverse args', () => { + const a = dayjs({ + year: 2011, + month: 9, + date: 12, + hour: 6, + minute: 7, + second: 8, + millisecond: 500 + }) + + expect(a.add({ millisecond: 50 }).millisecond()).toBe(550) + expect(a.add({ second: 1 }).second()).toBe(9) + expect(a.add({ minute: 1 }).minute()).toBe(8) + expect(a.add({ hour: 1 }).hour()).toBe(7) + expect(a.add({ day: 1 }).date()).toBe(13) + expect(a.add({ week: 1 }).date()).toBe(19) + expect(a.add({ month: 1 }).month()).toBe(9) + expect(a.add({ year: 1 }).year()).toBe(2012) + expect(a.add({ quarter: 1 }).month()).toBe(11) +}) + +it('add string long', () => { + const a = dayjs({ + year: 2011, + month: 9, + date: 12, + hour: 6, + minute: 7, + second: 8, + millisecond: 500 + }) + + expect(a.add(50, 'millisecond').millisecond()).toBe(550) + expect(a.add(1, 'second').second()).toBe(9) + expect(a.add(1, 'minute').minute()).toBe(8) + expect(a.add(1, 'hour').hour()).toBe(7) + expect(a.add(1, 'day').date()).toBe(13) + expect(a.add(1, 'week').date()).toBe(19) + expect(a.add(1, 'month').month()).toBe(9) + expect(a.add(1, 'year').year()).toBe(2012) + expect(a.add(1, 'quarter').month()).toBe(11) +}) + +it('add string long singular', () => { + const a = dayjs({ + year: 2011, + month: 9, + date: 12, + hour: 6, + minute: 7, + second: 8, + millisecond: 500 + }) + + expect(a.add(50, 'milliseconds').millisecond()).toBe(550) + expect(a.add(1, 'seconds').second()).toBe(9) + expect(a.add(1, 'minutes').minute()).toBe(8) + expect(a.add(1, 'hours').hour()).toBe(7) + expect(a.add(1, 'days').date()).toBe(13) + expect(a.add(1, 'weeks').date()).toBe(19) + expect(a.add(1, 'months').month()).toBe(9) + expect(a.add(1, 'years').year()).toBe(2012) + expect(a.add(1, 'quarters').month()).toBe(11) +}) + +it('add string short', () => { + const a = dayjs({ + year: 2011, + month: 9, + date: 12, + hour: 6, + minute: 7, + second: 8, + millisecond: 500 + }) + + expect(a.add(50, 'ms').millisecond()).toBe(550) + expect(a.add(1, 's').second()).toBe(9) + expect(a.add(1, 'm').minute()).toBe(8) + expect(a.add(1, 'h').hour()).toBe(7) + expect(a.add(1, 'd').date()).toBe(13) + expect(a.add(1, 'w').date()).toBe(19) + expect(a.add(1, 'M').month()).toBe(9) + expect(a.add(1, 'y').year()).toBe(2012) + expect(a.add(1, 'Q').month()).toBe(11) +}) + +it('add strings string short', () => { + const a = dayjs({ + year: 2011, + month: 9, + date: 12, + hour: 6, + minute: 7, + second: 8, + millisecond: 500 + }) + + expect(a.add('50', 'ms').millisecond()).toBe(550) + expect(a.add('1', 's').second()).toBe(9) + expect(a.add('1', 'm').minute()).toBe(8) + expect(a.add('1', 'h').hour()).toBe(7) + expect(a.add('1', 'd').date()).toBe(13) + expect(a.add('1', 'w').date()).toBe(19) + expect(a.add('1', 'M').month()).toBe(9) + expect(a.add('1', 'y').year()).toBe(2012) + expect(a.add('1', 'Q').month()).toBe(11) +}) + +it('add no string with milliseconds default', () => { + const a = dayjs({ + year: 2011, + month: 9, + date: 12, + hour: 6, + minute: 7, + second: 8, + millisecond: 500 + }) + + expect(a.add(50).millisecond()).toBe(550) +}) + +it('subtract strings string short', () => { + const a = dayjs({ + year: 2011, + month: 9, + date: 12, + hour: 6, + minute: 7, + second: 8, + millisecond: 500 + }) + expect(a.subtract('50', 'ms').millisecond()).toBe(450) + expect(a.subtract('1', 's').second()).toBe(7) + expect(a.subtract('1', 'm').minute()).toBe(6) + expect(a.subtract('1', 'h').hour()).toBe(5) + expect(a.subtract('1', 'd').date()).toBe(11) + expect(a.subtract('1', 'w').date()).toBe(5) + expect(a.subtract('1', 'M').month()).toBe(7) + expect(a.subtract('1', 'y').year()).toBe(2010) + expect(a.subtract('1', 'Q').month()).toBe(5) +}) + +it('add decimal values of days and months', () => { + expect(dayjs([2016, 4, 3]).add(1.6, 'days').date()).toBe(5) + expect(dayjs([2016, 4, 3]).add(-1.6, 'days').date()).toBe(1) + expect(dayjs([2016, 4, 1]).add(-1.6, 'days').date()).toBe(30) + expect(dayjs([2016, 4, 3]).add(1.6, 'months').month()).toBe(4) + expect(dayjs([2016, 4, 3]).add(-1.6, 'months').month()).toBe(1) + expect(dayjs([2016, 1, 3]).add(-1.6, 'months').month()).toBe(11) + expect(dayjs([2016, 4, 3]).subtract(1.6, 'days').date()).toBe(1) + expect(dayjs([2016, 4, 2]).subtract(1.6, 'days').date()).toBe(31) + expect(dayjs([2016, 2, 1]).subtract(1.1, 'days').date()).toBe(31) + expect(dayjs([2016, 4, 3]).subtract(-1.6, 'days').date()).toBe(5) + expect(dayjs([2016, 4, 30]).subtract(-1.6, 'days').date()).toBe(2) + expect(dayjs([2016, 4, 3]).subtract(1.6, 'months').month()).toBe(1) + expect(dayjs([2016, 4, 3]).subtract(-1.6, 'months').month()).toBe(4) + expect(dayjs([2016, 12, 31]).subtract(-1.6, 'months').month()).toBe(0) + expect(dayjs([2016, 1, 1]).add(1.6, 'years').format('YYYY-MM-DD')).toBe('2017-01-01') + expect(dayjs([2016, 7, 1]).add(1.6, 'years').format('YYYY-MM-DD')).toBe('2017-07-01') + expect(dayjs([2016, 1, 1]).add(1.1, 'quarters').format('YYYY-MM-DD')).toBe('2016-04-01') +}) diff --git a/types/plugin/objectSupport.d.ts b/types/plugin/objectSupport.d.ts new file mode 100755 index 000000000..65eb70dc2 --- /dev/null +++ b/types/plugin/objectSupport.d.ts @@ -0,0 +1,15 @@ +import { PluginFunc } from 'dayjs' + +declare const plugin: PluginFunc +export = plugin + +declare module 'dayjs' { + interface Dayjs { + setObject(argument: object): Dayjs + addObject(argument:object): Dayjs + set(argument: object): Dayjs + add(argument?: object): Dayjs + subtractObject(argument?: object): Dayjs + subtract(argument?: object): Dayjs + } +} From 89596ca3c3b7ea77c8b4506b77abd309cba47aab Mon Sep 17 00:00:00 2001 From: Zero no Overture Date: Tue, 28 Apr 2020 11:57:19 +0800 Subject: [PATCH 02/14] refactor:remove unnecessary proto methods --- src/plugin/objectSupport/index.js | 41 +++++++++++++++++-------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/src/plugin/objectSupport/index.js b/src/plugin/objectSupport/index.js index 66b24b424..b5ac82b46 100755 --- a/src/plugin/objectSupport/index.js +++ b/src/plugin/objectSupport/index.js @@ -34,17 +34,19 @@ export default (o, c) => { const isObject = obj => !(obj instanceof Date) && !(obj instanceof Array) && obj instanceof Object const parseDate = (cfg) => { const { date, utc } = cfg + // if (Array.isArray(date)) return parseArrayArgument([0, ...date], utc) if (isObject(date)) return parseObjectArgument(date, utc) return date } const oldParse = proto.parse proto.parse = function (cfg) { + // console.log(cfg) cfg.date = parseDate(cfg) oldParse.bind(this)(cfg) } - proto.setObject = function (argument) { + const setObject = function (argument) { const keys = Object.keys(argument) let chain = this.clone() keys.forEach((key) => { @@ -52,15 +54,7 @@ export default (o, c) => { }) return chain } - const oldSet = proto.set - proto.set = function (string, int) { - if (string instanceof Object) { - return this.setObject(string) - } - return oldSet.bind(this)(string, int) - } - - proto.addObject = function (argument) { + const addObject = function (argument) { const keys = Object.keys(argument) let chain = this keys.forEach((key) => { @@ -68,14 +62,8 @@ export default (o, c) => { }) return chain } - const oldAdd = proto.add - proto.add = function (number, string) { - if (number instanceof Object) { - return this.addObject(number) - } - return oldAdd.bind(this)(number, string) - } - proto.subtractObject = function (argument) { + + const subtractObject = function (argument) { const keys = Object.keys(argument) let chain = this keys.forEach((key) => { @@ -83,10 +71,25 @@ export default (o, c) => { }) return chain } + + const oldSet = proto.set + proto.set = function (string, int) { + if (string instanceof Object) { + return setObject(string) + } + return oldSet.bind(this)(string, int) + } + const oldAdd = proto.add + proto.add = function (number, string) { + if (number instanceof Object) { + return addObject(number) + } + return oldAdd.bind(this)(number, string) + } const oldSubtract = proto.subtract proto.subtract = function (number, string) { if (number instanceof Object) { - return this.subtractObject(number) + return subtractObject(number) } return oldSubtract.bind(this)(number, string) } From 59632416b0aa9842af0940571594a96ea6eb8d52 Mon Sep 17 00:00:00 2001 From: Zero no Overture Date: Tue, 28 Apr 2020 11:58:46 +0800 Subject: [PATCH 03/14] refactor:remove unnecessary proto methods --- types/plugin/objectSupport.d.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/types/plugin/objectSupport.d.ts b/types/plugin/objectSupport.d.ts index 65eb70dc2..bf9a666b9 100755 --- a/types/plugin/objectSupport.d.ts +++ b/types/plugin/objectSupport.d.ts @@ -5,11 +5,8 @@ export = plugin declare module 'dayjs' { interface Dayjs { - setObject(argument: object): Dayjs - addObject(argument:object): Dayjs set(argument: object): Dayjs add(argument?: object): Dayjs - subtractObject(argument?: object): Dayjs subtract(argument?: object): Dayjs } } From 9e8fef157eae55a4fd38bcc300cd77c35daace6d Mon Sep 17 00:00:00 2001 From: Zero no Overture Date: Tue, 28 Apr 2020 13:05:43 +0800 Subject: [PATCH 04/14] fix:action scope of this --- src/plugin/objectSupport/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/plugin/objectSupport/index.js b/src/plugin/objectSupport/index.js index b5ac82b46..866302d0f 100755 --- a/src/plugin/objectSupport/index.js +++ b/src/plugin/objectSupport/index.js @@ -75,21 +75,21 @@ export default (o, c) => { const oldSet = proto.set proto.set = function (string, int) { if (string instanceof Object) { - return setObject(string) + return setObject.bind(this)(string) } return oldSet.bind(this)(string, int) } const oldAdd = proto.add proto.add = function (number, string) { if (number instanceof Object) { - return addObject(number) + return addObject.bind(this)(number) } return oldAdd.bind(this)(number, string) } const oldSubtract = proto.subtract proto.subtract = function (number, string) { if (number instanceof Object) { - return subtractObject(number) + return subtractObject.bind(this)(number) } return oldSubtract.bind(this)(number, string) } From 1487db5ad0c09c0a24f564380fa1fc2d6a217074 Mon Sep 17 00:00:00 2001 From: Zero no Overture Date: Wed, 29 Apr 2020 11:07:28 +0800 Subject: [PATCH 05/14] fix:remove unnecessary symbols sorry, I was careless --- types/plugin/objectSupport.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/plugin/objectSupport.d.ts b/types/plugin/objectSupport.d.ts index bf9a666b9..0d3cf05b4 100755 --- a/types/plugin/objectSupport.d.ts +++ b/types/plugin/objectSupport.d.ts @@ -6,7 +6,7 @@ export = plugin declare module 'dayjs' { interface Dayjs { set(argument: object): Dayjs - add(argument?: object): Dayjs - subtract(argument?: object): Dayjs + add(argument: object): Dayjs + subtract(argument: object): Dayjs } } From fe7fb05a4798031eb989c910f51287672af8a243 Mon Sep 17 00:00:00 2001 From: Zero Date: Wed, 29 Apr 2020 14:49:25 +0800 Subject: [PATCH 06/14] refactor:optimized parseDate code --- src/plugin/objectSupport/index.js | 52 +++++++++++-------------------- test/plugin/objectSupport.test.js | 23 +++++++------- 2 files changed, 30 insertions(+), 45 deletions(-) diff --git a/src/plugin/objectSupport/index.js b/src/plugin/objectSupport/index.js index 866302d0f..67dc39523 100755 --- a/src/plugin/objectSupport/index.js +++ b/src/plugin/objectSupport/index.js @@ -1,41 +1,27 @@ +import U from '../../utils' + export default (o, c) => { const proto = c.prototype - - /** - * Array to date - * @param d - * @param utc - * @returns {Date} - */ - const parseArrayArgument = (d, utc) => { - if (utc) { - return new Date(Date.UTC(d[1], d[2] - 1, d[3] - || 1, d[4] || 0, d[5] || 0, d[6] || 0, d[7] || 0)) - } - return new Date(d[1], d[2] - 1, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, d[7] || 0) - } - - /** - * Converts the object to a date array - * @param d Object { years, months, date, hours, minutes, seconds, milliseconds} - * @param utc - */ - const parseObjectArgument = (d, utc) => parseArrayArgument([ - 0, - d.y || d.year || d.years || 1970, - d.M || d.month || d.months || 1, - d.d || d.day || d.days || d.date || 1, - d.h || d.hour || d.hours || 0, - d.m || d.minute || d.minutes || 0, - d.s || d.second || d.seconds || 0, - d.ms || d.millisecond || d.milliseconds || 0 - ], utc) - const isObject = obj => !(obj instanceof Date) && !(obj instanceof Array) && obj instanceof Object const parseDate = (cfg) => { const { date, utc } = cfg - // if (Array.isArray(date)) return parseArrayArgument([0, ...date], utc) - if (isObject(date)) return parseObjectArgument(date, utc) + const $d = {} + if (isObject(date)) { + Object.keys(date).forEach((k) => { + $d[U.p(k)] = date[k] + }) + const arr = [ + $d.year || 1970, + $d.month - 1 || 0, + $d.day || 1, + $d.hour || 0, + $d.minute || 0, + $d.second || 0, + $d.millisecond || 0 + ] + if (utc) return new Date(Date.UTC(...arr)) + return new Date(...arr) + } return date } diff --git a/test/plugin/objectSupport.test.js b/test/plugin/objectSupport.test.js index 15ad01675..8ca635f24 100755 --- a/test/plugin/objectSupport.test.js +++ b/test/plugin/objectSupport.test.js @@ -22,7 +22,6 @@ const tests = [ [{ year: 2010 }, '2010-01-01 00:00:00.000'], [{ year: 2010, month: 1 }, '2010-01-01 00:00:00.000'], [{ year: 2010, month: 1, day: 12 }, '2010-01-12 00:00:00.000'], - [{ year: 2010, month: 1, date: 12 }, '2010-01-12 00:00:00.000'], [ { year: 2010, month: 1, day: 12, hours: 1 @@ -31,7 +30,7 @@ const tests = [ ], [ { - year: 2010, month: 1, date: 12, hours: 1 + year: 2010, month: 1, day: 12, hours: 1 }, '2010-01-12 01:00:00.000' ], @@ -43,7 +42,7 @@ const tests = [ ], [ { - year: 2010, month: 1, date: 12, hours: 1, minutes: 1 + year: 2010, month: 1, day: 12, hours: 1, minutes: 1 }, '2010-01-12 01:01:00.000' ], @@ -122,7 +121,7 @@ it('add short reverse args', () => { const a = dayjs({ year: 2011, month: 9, - date: 12, + day: 12, hour: 6, minute: 7, second: 8, @@ -155,7 +154,7 @@ it('add long reverse args', () => { const a = dayjs({ year: 2011, month: 9, - date: 12, + day: 12, hour: 6, minute: 7, second: 8, @@ -177,7 +176,7 @@ it('add long singular reverse args', () => { const a = dayjs({ year: 2011, month: 9, - date: 12, + day: 12, hour: 6, minute: 7, second: 8, @@ -199,7 +198,7 @@ it('add string long', () => { const a = dayjs({ year: 2011, month: 9, - date: 12, + day: 12, hour: 6, minute: 7, second: 8, @@ -221,7 +220,7 @@ it('add string long singular', () => { const a = dayjs({ year: 2011, month: 9, - date: 12, + day: 12, hour: 6, minute: 7, second: 8, @@ -243,7 +242,7 @@ it('add string short', () => { const a = dayjs({ year: 2011, month: 9, - date: 12, + day: 12, hour: 6, minute: 7, second: 8, @@ -265,7 +264,7 @@ it('add strings string short', () => { const a = dayjs({ year: 2011, month: 9, - date: 12, + day: 12, hour: 6, minute: 7, second: 8, @@ -287,7 +286,7 @@ it('add no string with milliseconds default', () => { const a = dayjs({ year: 2011, month: 9, - date: 12, + day: 12, hour: 6, minute: 7, second: 8, @@ -301,7 +300,7 @@ it('subtract strings string short', () => { const a = dayjs({ year: 2011, month: 9, - date: 12, + day: 12, hour: 6, minute: 7, second: 8, From 7d7db507d0384ece954b89e61fae36ee72c70da3 Mon Sep 17 00:00:00 2001 From: Zero Date: Wed, 29 Apr 2020 15:52:16 +0800 Subject: [PATCH 07/14] refactor:optimized code --- src/plugin/objectSupport/index.js | 10 ++++++---- test/plugin/objectSupport.test.js | 23 ++++++++++++----------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/plugin/objectSupport/index.js b/src/plugin/objectSupport/index.js index 67dc39523..76e464ea6 100755 --- a/src/plugin/objectSupport/index.js +++ b/src/plugin/objectSupport/index.js @@ -1,14 +1,16 @@ -import U from '../../utils' - export default (o, c) => { const proto = c.prototype const isObject = obj => !(obj instanceof Date) && !(obj instanceof Array) && obj instanceof Object + const prettyUnit = (u) => { + const unit = proto.$utils().p(u) + return unit === 'date' ? 'day' : unit + } const parseDate = (cfg) => { const { date, utc } = cfg const $d = {} if (isObject(date)) { Object.keys(date).forEach((k) => { - $d[U.p(k)] = date[k] + $d[prettyUnit(k)] = date[k] }) const arr = [ $d.year || 1970, @@ -28,7 +30,7 @@ export default (o, c) => { const oldParse = proto.parse proto.parse = function (cfg) { // console.log(cfg) - cfg.date = parseDate(cfg) + cfg.date = parseDate.bind(this)(cfg) oldParse.bind(this)(cfg) } diff --git a/test/plugin/objectSupport.test.js b/test/plugin/objectSupport.test.js index 8ca635f24..15ad01675 100755 --- a/test/plugin/objectSupport.test.js +++ b/test/plugin/objectSupport.test.js @@ -22,6 +22,7 @@ const tests = [ [{ year: 2010 }, '2010-01-01 00:00:00.000'], [{ year: 2010, month: 1 }, '2010-01-01 00:00:00.000'], [{ year: 2010, month: 1, day: 12 }, '2010-01-12 00:00:00.000'], + [{ year: 2010, month: 1, date: 12 }, '2010-01-12 00:00:00.000'], [ { year: 2010, month: 1, day: 12, hours: 1 @@ -30,7 +31,7 @@ const tests = [ ], [ { - year: 2010, month: 1, day: 12, hours: 1 + year: 2010, month: 1, date: 12, hours: 1 }, '2010-01-12 01:00:00.000' ], @@ -42,7 +43,7 @@ const tests = [ ], [ { - year: 2010, month: 1, day: 12, hours: 1, minutes: 1 + year: 2010, month: 1, date: 12, hours: 1, minutes: 1 }, '2010-01-12 01:01:00.000' ], @@ -121,7 +122,7 @@ it('add short reverse args', () => { const a = dayjs({ year: 2011, month: 9, - day: 12, + date: 12, hour: 6, minute: 7, second: 8, @@ -154,7 +155,7 @@ it('add long reverse args', () => { const a = dayjs({ year: 2011, month: 9, - day: 12, + date: 12, hour: 6, minute: 7, second: 8, @@ -176,7 +177,7 @@ it('add long singular reverse args', () => { const a = dayjs({ year: 2011, month: 9, - day: 12, + date: 12, hour: 6, minute: 7, second: 8, @@ -198,7 +199,7 @@ it('add string long', () => { const a = dayjs({ year: 2011, month: 9, - day: 12, + date: 12, hour: 6, minute: 7, second: 8, @@ -220,7 +221,7 @@ it('add string long singular', () => { const a = dayjs({ year: 2011, month: 9, - day: 12, + date: 12, hour: 6, minute: 7, second: 8, @@ -242,7 +243,7 @@ it('add string short', () => { const a = dayjs({ year: 2011, month: 9, - day: 12, + date: 12, hour: 6, minute: 7, second: 8, @@ -264,7 +265,7 @@ it('add strings string short', () => { const a = dayjs({ year: 2011, month: 9, - day: 12, + date: 12, hour: 6, minute: 7, second: 8, @@ -286,7 +287,7 @@ it('add no string with milliseconds default', () => { const a = dayjs({ year: 2011, month: 9, - day: 12, + date: 12, hour: 6, minute: 7, second: 8, @@ -300,7 +301,7 @@ it('subtract strings string short', () => { const a = dayjs({ year: 2011, month: 9, - day: 12, + date: 12, hour: 6, minute: 7, second: 8, From 60f59be761ec91550e35ad1d1cad3296b2192770 Mon Sep 17 00:00:00 2001 From: Zero Date: Wed, 29 Apr 2020 16:24:14 +0800 Subject: [PATCH 08/14] refactor:removed ...arr --- src/plugin/objectSupport/index.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/plugin/objectSupport/index.js b/src/plugin/objectSupport/index.js index 76e464ea6..e838b3cfc 100755 --- a/src/plugin/objectSupport/index.js +++ b/src/plugin/objectSupport/index.js @@ -12,7 +12,7 @@ export default (o, c) => { Object.keys(date).forEach((k) => { $d[prettyUnit(k)] = date[k] }) - const arr = [ + const d = [ $d.year || 1970, $d.month - 1 || 0, $d.day || 1, @@ -21,8 +21,10 @@ export default (o, c) => { $d.second || 0, $d.millisecond || 0 ] - if (utc) return new Date(Date.UTC(...arr)) - return new Date(...arr) + if (utc) { + return new Date(Date.UTC(d[0], d[1], d[2], d[3], d[4], d[5], d[6])) + } + return new Date(d[0], d[1], d[2], d[3], d[4], d[5], d[6]) } return date } From 460e312df884e6725a92a93ab8ff889bdae9895c Mon Sep 17 00:00:00 2001 From: Zero Date: Wed, 29 Apr 2020 16:38:49 +0800 Subject: [PATCH 09/14] refactor:reduce the compression --- src/plugin/objectSupport/index.js | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/plugin/objectSupport/index.js b/src/plugin/objectSupport/index.js index e838b3cfc..bab13d6b7 100755 --- a/src/plugin/objectSupport/index.js +++ b/src/plugin/objectSupport/index.js @@ -12,19 +12,17 @@ export default (o, c) => { Object.keys(date).forEach((k) => { $d[prettyUnit(k)] = date[k] }) - const d = [ - $d.year || 1970, - $d.month - 1 || 0, - $d.day || 1, - $d.hour || 0, - $d.minute || 0, - $d.second || 0, - $d.millisecond || 0 - ] + const y = $d.year || 1970 + const M = $d.month - 1 || 0 + const d = $d.day || 1 + const h = $d.hour || 0 + const m = $d.minute || 0 + const s = $d.second || 0 + const ms = $d.millisecond || 0 if (utc) { - return new Date(Date.UTC(d[0], d[1], d[2], d[3], d[4], d[5], d[6])) + return new Date(Date.UTC(y, M, d, h, m, s, ms)) } - return new Date(d[0], d[1], d[2], d[3], d[4], d[5], d[6]) + return new Date(y, M, d, h, m, s, ms) } return date } From fa8528de64310fd5735a5f325108351cc90261a0 Mon Sep 17 00:00:00 2001 From: Zero Date: Wed, 29 Apr 2020 17:39:29 +0800 Subject: [PATCH 10/14] test:add default year,month,date --- test/plugin/objectSupport.test.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/plugin/objectSupport.test.js b/test/plugin/objectSupport.test.js index 15ad01675..078ff192a 100755 --- a/test/plugin/objectSupport.test.js +++ b/test/plugin/objectSupport.test.js @@ -23,6 +23,11 @@ const tests = [ [{ year: 2010, month: 1 }, '2010-01-01 00:00:00.000'], [{ year: 2010, month: 1, day: 12 }, '2010-01-12 00:00:00.000'], [{ year: 2010, month: 1, date: 12 }, '2010-01-12 00:00:00.000'], + [ + { + hour: 15, minute: 25, second: 50, millisecond: 125 + }, + '1970-01-01 15:25:50.125'], [ { year: 2010, month: 1, day: 12, hours: 1 From b9f7f465b2e203a0374c0fa1a1c12ae7193ab56b Mon Sep 17 00:00:00 2001 From: Zero Date: Wed, 29 Apr 2020 22:57:42 +0800 Subject: [PATCH 11/14] refactor:redundant methods --- src/plugin/objectSupport/index.js | 55 ++++++++++--------------------- 1 file changed, 17 insertions(+), 38 deletions(-) diff --git a/src/plugin/objectSupport/index.js b/src/plugin/objectSupport/index.js index bab13d6b7..21bcd21d2 100755 --- a/src/plugin/objectSupport/index.js +++ b/src/plugin/objectSupport/index.js @@ -29,56 +29,35 @@ export default (o, c) => { const oldParse = proto.parse proto.parse = function (cfg) { - // console.log(cfg) cfg.date = parseDate.bind(this)(cfg) oldParse.bind(this)(cfg) } - const setObject = function (argument) { - const keys = Object.keys(argument) - let chain = this.clone() - keys.forEach((key) => { - chain = chain.$set(key, argument[key]) - }) - return chain - } - const addObject = function (argument) { - const keys = Object.keys(argument) - let chain = this - keys.forEach((key) => { - chain = chain.add(argument[key], key) - }) - return chain + const oldSet = function (int, string) { + return this.clone().$set(string, int) } + const oldAdd = proto.add - const subtractObject = function (argument) { - const keys = Object.keys(argument) - let chain = this - keys.forEach((key) => { - chain = chain.subtract(argument[key], key) - }) - return chain + const callObject = function (call, argument, string, offset = 1) { + if (argument instanceof Object) { + const keys = Object.keys(argument) + let chain = this + keys.forEach((key) => { + chain = call.bind(chain)(argument[key] * offset, key) + }) + return chain + } + return call.bind(this)(argument * offset, string) } - const oldSet = proto.set proto.set = function (string, int) { - if (string instanceof Object) { - return setObject.bind(this)(string) - } - return oldSet.bind(this)(string, int) + int = int === undefined ? string : int + return callObject.bind(this)(oldSet, int, string) } - const oldAdd = proto.add proto.add = function (number, string) { - if (number instanceof Object) { - return addObject.bind(this)(number) - } - return oldAdd.bind(this)(number, string) + return callObject.bind(this)(oldAdd, number, string) } - const oldSubtract = proto.subtract proto.subtract = function (number, string) { - if (number instanceof Object) { - return subtractObject.bind(this)(number) - } - return oldSubtract.bind(this)(number, string) + return callObject.bind(this)(oldAdd, number, string, -1) } } From 46ecb61bc1085d315e79725bf466c2b2b57c3eee Mon Sep 17 00:00:00 2001 From: Zero Date: Wed, 29 Apr 2020 23:15:03 +0800 Subject: [PATCH 12/14] test:relativeTime line 23 covered --- test/plugin/relativeTime.test.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/plugin/relativeTime.test.js b/test/plugin/relativeTime.test.js index e8a26e0a3..7c84f71c6 100644 --- a/test/plugin/relativeTime.test.js +++ b/test/plugin/relativeTime.test.js @@ -5,6 +5,8 @@ import * as C from '../../src/constant' import relativeTime from '../../src/plugin/relativeTime' import utc from '../../src/plugin/utc' import '../../src/locale/ru' +import en from '../../locale/en' + dayjs.extend(relativeTime) @@ -137,3 +139,9 @@ it('Custom thresholds and rounding support', () => { }) expect(dayjs().subtract(45, 'm').fromNow()).toBe('45 minutes ago') }) + +it('Set the default language to en->There is no relativeTime property', () => { + const d = dayjs() + d.locale(en) + expect(d.subtract(45, 'm').fromNow('45 minutes ago')) +}) From 3c51c1c2165d56426af5f4f2ec07862eba7e26ad Mon Sep 17 00:00:00 2001 From: Zero Date: Wed, 29 Apr 2020 23:19:35 +0800 Subject: [PATCH 13/14] Revert "test:relativeTime line 23 covered" This reverts commit 46ecb61b --- test/plugin/relativeTime.test.js | 8 -------- 1 file changed, 8 deletions(-) diff --git a/test/plugin/relativeTime.test.js b/test/plugin/relativeTime.test.js index 7c84f71c6..e8a26e0a3 100644 --- a/test/plugin/relativeTime.test.js +++ b/test/plugin/relativeTime.test.js @@ -5,8 +5,6 @@ import * as C from '../../src/constant' import relativeTime from '../../src/plugin/relativeTime' import utc from '../../src/plugin/utc' import '../../src/locale/ru' -import en from '../../locale/en' - dayjs.extend(relativeTime) @@ -139,9 +137,3 @@ it('Custom thresholds and rounding support', () => { }) expect(dayjs().subtract(45, 'm').fromNow()).toBe('45 minutes ago') }) - -it('Set the default language to en->There is no relativeTime property', () => { - const d = dayjs() - d.locale(en) - expect(d.subtract(45, 'm').fromNow('45 minutes ago')) -}) From 83f32066922e61d04c05922536e9170562ab1214 Mon Sep 17 00:00:00 2001 From: Zero Date: Thu, 30 Apr 2020 15:06:09 +0800 Subject: [PATCH 14/14] refactor:optimized code --- src/plugin/objectSupport/index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) mode change 100755 => 100644 src/plugin/objectSupport/index.js diff --git a/src/plugin/objectSupport/index.js b/src/plugin/objectSupport/index.js old mode 100755 new mode 100644 index 21bcd21d2..9eab4acd5 --- a/src/plugin/objectSupport/index.js +++ b/src/plugin/objectSupport/index.js @@ -33,9 +33,7 @@ export default (o, c) => { oldParse.bind(this)(cfg) } - const oldSet = function (int, string) { - return this.clone().$set(string, int) - } + const oldSet = proto.set const oldAdd = proto.add const callObject = function (call, argument, string, offset = 1) { @@ -52,7 +50,9 @@ export default (o, c) => { proto.set = function (string, int) { int = int === undefined ? string : int - return callObject.bind(this)(oldSet, int, string) + return callObject.bind(this)(function (i, s) { + return oldSet.bind(this)(s, i) + }, int, string) } proto.add = function (number, string) { return callObject.bind(this)(oldAdd, number, string)