From 21459770139c8995a9bde9f09c7912ac8c0b1e85 Mon Sep 17 00:00:00 2001 From: iamkun Date: Fri, 18 May 2018 15:14:07 +0800 Subject: [PATCH 1/5] test: add test case fix #138 --- test/parse.test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/parse.test.js b/test/parse.test.js index b9f9d5324..09b046c91 100644 --- a/test/parse.test.js +++ b/test/parse.test.js @@ -29,6 +29,8 @@ describe('Parse', () => { expect(dayjs(d).valueOf()).toBe(moment(d).valueOf()) // not recommend d = '2018-4-1 1:1:1:223' expect(dayjs(d).valueOf()).toBe(moment(d).valueOf()) // not recommend + d = '2018-01' + expect(dayjs(d).valueOf()).toBe(moment(d).valueOf()) // not recommend }) it('String ISO 8601 date, time and zone', () => { From 79f6c0c9d244c80c5737d22f4c504ce006d3e666 Mon Sep 17 00:00:00 2001 From: iamkun Date: Fri, 18 May 2018 15:22:31 +0800 Subject: [PATCH 2/5] test: add test case --- src/constant.js | 2 +- src/index.js | 2 +- test/parse.test.js | 2 ++ 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/constant.js b/src/constant.js index da0661d44..9b44afdaa 100644 --- a/src/constant.js +++ b/src/constant.js @@ -24,7 +24,7 @@ export const DATE = 'date' export const FORMAT_DEFAULT = 'YYYY-MM-DDTHH:mm:ssZ' // regex -export const REGEX_PARSE = /^(\d{4})-?(\d{0,2})-?(\d{0,2})(.*?(\d{1,2}):(\d{1,2}):(\d{1,2}))?.?(\d{1,3})?$/ +export const REGEX_PARSE = /^(\d{4})-?(\d{1,2})-?(\d{0,2})(.*?(\d{1,2}):(\d{1,2}):(\d{1,2}))?.?(\d{1,3})?$/ export const REGEX_FORMAT = /\[.*?\]|Y{2,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g export const en = { diff --git a/src/index.js b/src/index.js index 12db1c5f9..69942b92c 100644 --- a/src/index.js +++ b/src/index.js @@ -52,7 +52,7 @@ const parseDate = (date) => { if ((typeof date === 'string') && (reg = date.match(C.REGEX_PARSE))) { // 2018-08-08 or 20180808 return new Date( - reg[1], (reg[2] - 1) || 0, reg[3] || 1, + reg[1], reg[2] - 1, reg[3] || 1, reg[5] || 0, reg[6] || 0, reg[7] || 0, reg[8] || 0 ) } diff --git a/test/parse.test.js b/test/parse.test.js index 09b046c91..0a2708d89 100644 --- a/test/parse.test.js +++ b/test/parse.test.js @@ -31,6 +31,8 @@ describe('Parse', () => { expect(dayjs(d).valueOf()).toBe(moment(d).valueOf()) // not recommend d = '2018-01' expect(dayjs(d).valueOf()).toBe(moment(d).valueOf()) // not recommend + d = '2018' + expect(dayjs(d).format()).toBe(moment(d).format()) // not recommend }) it('String ISO 8601 date, time and zone', () => { From 5e888e27894dc0609d220977a5e1407312c9a9a8 Mon Sep 17 00:00:00 2001 From: iamkun Date: Fri, 18 May 2018 15:42:53 +0800 Subject: [PATCH 3/5] refactor: refactor add() --- src/index.js | 12 ++++++------ test/manipulate.test.js | 2 ++ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index 69942b92c..37f398079 100644 --- a/src/index.js +++ b/src/index.js @@ -230,15 +230,15 @@ class Dayjs { number = Number(number) // eslint-disable-line no-param-reassign // units === 'ms' hard code here, will update in next release const unit = (units && (units.length === 1 || units === 'ms')) ? units : Utils.prettyUnit(units) + const instanceFactory = (u, n) => { + const date = this.set(C.DATE, 1).set(u, n + number) + return date.set(C.DATE, Math.min(this.$D, date.daysInMonth())) + } if (['M', C.M].indexOf(unit) > -1) { - let date = this.set(C.DATE, 1).set(C.M, this.$M + number) - date = date.set(C.DATE, Math.min(this.$D, date.daysInMonth())) - return date + return instanceFactory(C.M, this.$M) } if (['y', C.Y].indexOf(unit) > -1) { - let date = this.set(C.DATE, 1).set(C.Y, this.$y + number) - date = date.set(C.DATE, Math.min(this.$D, date.daysInMonth())) - return date + return instanceFactory(C.Y, this.$y) } let step switch (unit) { diff --git a/test/manipulate.test.js b/test/manipulate.test.js index 76e87d278..6a85769ff 100644 --- a/test/manipulate.test.js +++ b/test/manipulate.test.js @@ -43,6 +43,8 @@ it('Add Time days', () => { expect(dayjs().add(1, 'M').valueOf()).toBe(moment().add(1, 'M').valueOf()) expect(dayjs().add(1, 'y').valueOf()).toBe(moment().add(1, 'y').valueOf()) expect(dayjs('20111031').add(1, 'months').valueOf()).toBe(moment('20111031').add(1, 'months').valueOf()) + expect(dayjs('20160131').add(1, 'months').valueOf()).toBe(moment('20160131').add(1, 'months').valueOf()) + expect(dayjs('20160229').add(1, 'year').valueOf()).toBe(moment('20160229').add(1, 'year').valueOf()) expect(dayjs().add('2', 'years').valueOf()).toBe(moment().add('2', 'years').valueOf()) }) From 7d7b4ccb948f0acfad6a8ab001fa76ee8f004ae8 Mon Sep 17 00:00:00 2001 From: iamkun Date: Fri, 18 May 2018 15:46:24 +0800 Subject: [PATCH 4/5] refactor: ref utils --- src/utils.js | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/utils.js b/src/utils.js index de18ac760..bf92f4b83 100644 --- a/src/utils.js +++ b/src/utils.js @@ -15,16 +15,9 @@ const monthDiff = (a, b) => { // function from moment.js in order to keep the same result const wholeMonthDiff = ((b.year() - a.year()) * 12) + (b.month() - a.month()) const anchor = a.clone().add(wholeMonthDiff, 'months') - let anchor2 - let adjust - if (b - anchor < 0) { - anchor2 = a.clone().add(wholeMonthDiff - 1, 'months') - adjust = (b - anchor) / (anchor - anchor2) - } else { - anchor2 = a.clone().add(wholeMonthDiff + 1, 'months') - adjust = (b - anchor) / (anchor2 - anchor) - } - return Number(-(wholeMonthDiff + adjust)) + const c = b - anchor < 0 + const anchor2 = a.clone().add(wholeMonthDiff + (c ? -1 : 1), 'months') + return Number(-(wholeMonthDiff + ((b - anchor) / (c ? (anchor - anchor2) : (anchor2 - anchor))))) } const absFloor = n => (n < 0 ? Math.ceil(n) || 0 : Math.floor(n)) From 6b619ceace44704fe8bfe697d5e114a38e2ab43c Mon Sep 17 00:00:00 2001 From: iamkun Date: Fri, 18 May 2018 15:55:09 +0800 Subject: [PATCH 5/5] chore: optional type definition --- index.d.ts | 2 +- package.json | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index fca4601d4..01f40918e 100644 --- a/index.d.ts +++ b/index.d.ts @@ -87,7 +87,7 @@ declare namespace dayjs { export type PluginFunc = (option: ConfigType, d1: Dayjs, d2: Dayjs) => void - export function extend(plugin: PluginFunc, option: ConfigType): Dayjs + export function extend(plugin: PluginFunc, option?: ConfigType): Dayjs export function local(arg1: any, arg2?: any): void } diff --git a/package.json b/package.json index 73f3e80be..3db9749b3 100644 --- a/package.json +++ b/package.json @@ -71,6 +71,7 @@ "pre-commit": "^1.2.2", "rollup": "^0.57.1", "rollup-plugin-babel": "^4.0.0-beta.4", - "rollup-plugin-uglify": "^3.0.0" + "rollup-plugin-uglify": "^3.0.0", + "typescript": "^2.8.3" } }