From e173abbe7297968db158efacf75971b4c832881a Mon Sep 17 00:00:00 2001 From: iamkun Date: Thu, 19 Apr 2018 17:14:27 +0800 Subject: [PATCH 01/12] style(moment style): moment style --- src/index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 596076d5d..2a9759168 100644 --- a/src/index.js +++ b/src/index.js @@ -193,10 +193,10 @@ class Dayjs { }) } - diff(otherDate, unit, float = false) { - const other = otherDate instanceof Dayjs ? otherDate : new Dayjs(otherDate) - const diff = this - other - let result = Utils.monthDiff(this, other) + diff(input, unit, float = false) { + const that = input instanceof Dayjs ? input : new Dayjs(input) + const diff = this - that + let result = Utils.monthDiff(this, that) switch (unit) { case 'years': result /= 12 From b56a83b5aa09e1d6167906766f56f80f248f05a0 Mon Sep 17 00:00:00 2001 From: iamkun Date: Thu, 19 Apr 2018 17:43:48 +0800 Subject: [PATCH 02/12] style(modern): modern --- src/constant.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/constant.js b/src/constant.js index 989142820..88c63d773 100644 --- a/src/constant.js +++ b/src/constant.js @@ -3,7 +3,7 @@ export const SECONDS_A_HOUR = SECONDS_A_MINUTE * 60 export const SECONDS_A_DAY = SECONDS_A_HOUR * 24 export const SECONDS_A_WEEK = SECONDS_A_DAY * 7 -export const MILLISECONDS_A_SECOND = 1000 +export const MILLISECONDS_A_SECOND = 1e3 export const MILLISECONDS_A_MINUTE = SECONDS_A_MINUTE * MILLISECONDS_A_SECOND export const MILLISECONDS_A_HOUR = SECONDS_A_HOUR * MILLISECONDS_A_SECOND export const MILLISECONDS_A_DAY = SECONDS_A_DAY * MILLISECONDS_A_SECOND From c702be8ecaa7ddb163975b9f53b40c1b716c58f8 Mon Sep 17 00:00:00 2001 From: iamkun Date: Thu, 19 Apr 2018 18:32:01 +0800 Subject: [PATCH 03/12] build(rename): rename build dist file name --- rollup.config.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rollup.config.js b/rollup.config.js index 00b49f670..6a1fc4d54 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,10 +1,11 @@ import babel from 'rollup-plugin-babel' import uglify from 'rollup-plugin-uglify' +import packageInfo from './package.json'; export default { input: 'src/index.js', output: { - file: 'dist/index.js', + file: `dist/${packageInfo.name}.min.js`, format: 'umd', name: 'dayjs' }, From bdc33b2ce392194b91f70a04161616bcc9ca8bdb Mon Sep 17 00:00:00 2001 From: iamkun Date: Fri, 20 Apr 2018 09:43:16 +0800 Subject: [PATCH 04/12] fix(immutable): immutable set --- src/index.js | 14 ++++++++------ test/get-set.test.js | 15 +++++++++++++-- test/parse.test.js | 9 ++++----- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/index.js b/src/index.js index 2a9759168..69f11b0d4 100644 --- a/src/index.js +++ b/src/index.js @@ -96,8 +96,7 @@ class Dayjs { return this.startOf(arg, false) } - set(string, int) { - if (!Utils.isNumber(int)) return this + mSet(string, int) { switch (string) { case 'date': this.$date.setDate(int) @@ -115,12 +114,15 @@ class Dayjs { return this } + set(string, int) { + if (!Utils.isNumber(int)) return this + return this.clone().mSet(string, int) + } + add(number, string) { if (['M', 'months'].indexOf(string) > -1) { - const date = this.clone() - date.set('date', 1) - date.set('month', this.month() + number) - date.set('date', Math.min(this.date(), date.daysInMonth())) + let date = this.set('date', 1).set('month', this.month() + number) + date = date.set('date', Math.min(this.date(), date.daysInMonth())) return date } let step diff --git a/test/get-set.test.js b/test/get-set.test.js index f6ca4ed97..b09ae1acf 100644 --- a/test/get-set.test.js +++ b/test/get-set.test.js @@ -23,12 +23,23 @@ it('Date', () => { }) it('Set Unknown String', () => { - expect(dayjs().set('Unknown String', 1).unix()) + const newDate = dayjs().set('Unknown String', 1) + expect(newDate.unix()) .toBe(moment().set('Unknown String', 1).unix()) }) it('Set Not Int', () => { - expect(dayjs().set('year', 'Not Int').unix()) + const newDate = dayjs().set('year', 'Not Int') + expect(newDate.unix()) .toBe(moment().set('year', 'Not Int').unix()) }) +it('Immutable Set', () => { + const dayjsA = dayjs() + const dayjsB = dayjsA.set('year', 2011) + const momentA = moment() + const momentB = momentA.set('year', 2011) + expect(dayjsA.unix()).not.toBe(dayjsB.unix()) + expect(momentA.unix()).toBe(momentB.unix()) +}) + diff --git a/test/parse.test.js b/test/parse.test.js index 3dcd27c1e..0b26fd2e0 100644 --- a/test/parse.test.js +++ b/test/parse.test.js @@ -37,15 +37,14 @@ it('Unix Timestamp Number (milliseconds) 1523520536000', () => { it('Clone not affect each other', () => { const base = dayjs(20170101) const year = base.year() - const another = base.clone() - another.set('year', year + 1) + const another = base.set('year', year + 1) expect(another.unix() - base.unix()).toBe(31536000) }) it('Clone with same value', () => { const base = dayjs() const year = base.year() - base.set('year', year + 1) - const another = base.clone() - expect(base.toString()).toBe(another.toString()) + const newBase = base.set('year', year + 1) + const another = newBase.clone() + expect(newBase.toString()).toBe(another.toString()) }) From a9f2694ba8ae38867976401c2f2b50dd2360c97b Mon Sep 17 00:00:00 2001 From: iamkun Date: Fri, 20 Apr 2018 09:46:16 +0800 Subject: [PATCH 05/12] style(short): short --- src/index.js | 58 ++++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/src/index.js b/src/index.js index 69f11b0d4..0ee59e8a7 100644 --- a/src/index.js +++ b/src/index.js @@ -23,42 +23,42 @@ class Dayjs { init() { this.timeZone = this.$date.getTimezoneOffset() / 60 this.timeZoneString = Utils.padStart(String(this.timeZone * -1).replace(/^(.)?(\d)/, '$10$200'), 5, '+') - this.$year = this.$date.getFullYear() - this.$month = this.$date.getMonth() - this.$day = this.$date.getDate() - this.$week = this.$date.getDay() - this.$hour = this.$date.getHours() - this.$minute = this.$date.getMinutes() - this.$second = this.$date.getSeconds() - this.$milliseconds = this.$date.getMilliseconds() + this.$y = this.$date.getFullYear() + this.$M = this.$date.getMonth() + this.$D = this.$date.getDate() + this.$W = this.$date.getDay() + this.$H = this.$date.getHours() + this.$m = this.$date.getMinutes() + this.$s = this.$date.getSeconds() + this.$ms = this.$date.getMilliseconds() } year() { - return this.$year + return this.$y } month() { - return this.$month + return this.$M } date() { - return this.$day + return this.$D } hour() { - return this.$hour + return this.$H } minute() { - return this.$minute + return this.$m } second() { - return this.$second + return this.$s } millisecond() { - return this.$milliseconds + return this.$ms } unix() { @@ -160,33 +160,33 @@ class Dayjs { return formatStr.replace(/Y{2,4}|M{1,2}|D{1,2}|d{1,4}|H{1,2}|m{1,2}|s{1,2}|Z{1,2}/g, (match) => { switch (match) { case 'YY': - return String(this.$year).slice(-2) + return String(this.$y).slice(-2) case 'YYYY': - return String(this.$year) + return String(this.$y) case 'M': - return String(this.$month + 1) + return String(this.$M + 1) case 'MM': - return Utils.padStart(String(this.$month + 1), 2, '0') + return Utils.padStart(String(this.$M + 1), 2, '0') case 'D': - return String(this.$day) + return String(this.$D) case 'DD': - return Utils.padStart(String(this.$day), 2, '0') + return Utils.padStart(String(this.$D), 2, '0') case 'd': - return String(this.$week) + return String(this.$W) case 'dddd': - return weeks[this.$week] + return weeks[this.$W] case 'H': - return String(this.$hour) + return String(this.$H) case 'HH': - return Utils.padStart(String(this.$hour), 2, '0') + return Utils.padStart(String(this.$H), 2, '0') case 'm': - return String(this.$minute) + return String(this.$m) case 'mm': - return Utils.padStart(String(this.$minute), 2, '0') + return Utils.padStart(String(this.$m), 2, '0') case 's': - return String(this.$second) + return String(this.$s) case 'ss': - return Utils.padStart(String(this.$second), 2, '0') + return Utils.padStart(String(this.$s), 2, '0') case 'Z': return `${this.timeZoneString.slice(0, -2)}:00` default: // 'ZZ' From a59353e8dbf575738f3a770842a6086e109b7ea6 Mon Sep 17 00:00:00 2001 From: iamkun Date: Fri, 20 Apr 2018 09:47:10 +0800 Subject: [PATCH 06/12] style(short): short date --- src/index.js | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/index.js b/src/index.js index 0ee59e8a7..e120e7419 100644 --- a/src/index.js +++ b/src/index.js @@ -16,21 +16,21 @@ const parseConfig = (config) => { class Dayjs { constructor(config) { - this.$date = parseConfig(config) + this.$d = parseConfig(config) this.init() } init() { - this.timeZone = this.$date.getTimezoneOffset() / 60 + this.timeZone = this.$d.getTimezoneOffset() / 60 this.timeZoneString = Utils.padStart(String(this.timeZone * -1).replace(/^(.)?(\d)/, '$10$200'), 5, '+') - this.$y = this.$date.getFullYear() - this.$M = this.$date.getMonth() - this.$D = this.$date.getDate() - this.$W = this.$date.getDay() - this.$H = this.$date.getHours() - this.$m = this.$date.getMinutes() - this.$s = this.$date.getSeconds() - this.$ms = this.$date.getMilliseconds() + this.$y = this.$d.getFullYear() + this.$M = this.$d.getMonth() + this.$D = this.$d.getDate() + this.$W = this.$d.getDay() + this.$H = this.$d.getHours() + this.$m = this.$d.getMinutes() + this.$s = this.$d.getSeconds() + this.$ms = this.$d.getMilliseconds() } year() { @@ -67,7 +67,7 @@ class Dayjs { valueOf() { // timezone(hour) * 60 * 60 * 1000 => ms - return this.$date.getTime() + return this.$d.getTime() } startOf(arg, isStartOf = true) { @@ -99,13 +99,13 @@ class Dayjs { mSet(string, int) { switch (string) { case 'date': - this.$date.setDate(int) + this.$d.setDate(int) break case 'month': - this.$date.setMonth(int) + this.$d.setMonth(int) break case 'year': - this.$date.setFullYear(int) + this.$d.setFullYear(int) break default: break @@ -232,7 +232,7 @@ class Dayjs { } toDate() { - return new Date(this.$date) + return new Date(this.$d) } toArray() { @@ -268,7 +268,7 @@ class Dayjs { } toString() { - return this.$date.toUTCString() + return this.$d.toUTCString() } } From 99e569a4df66dc456db18bd6a50761d0518f804b Mon Sep 17 00:00:00 2001 From: iamkun Date: Fri, 20 Apr 2018 10:01:03 +0800 Subject: [PATCH 07/12] refactor(format): format units --- src/constant.js | 11 +++++++ src/index.js | 70 ++++++++++++++++++++++------------------- src/utils.js | 1 + test/manipulate.test.js | 4 +-- 4 files changed, 51 insertions(+), 35 deletions(-) diff --git a/src/constant.js b/src/constant.js index 88c63d773..b64692a78 100644 --- a/src/constant.js +++ b/src/constant.js @@ -8,3 +8,14 @@ export const MILLISECONDS_A_MINUTE = SECONDS_A_MINUTE * MILLISECONDS_A_SECOND export const MILLISECONDS_A_HOUR = SECONDS_A_HOUR * MILLISECONDS_A_SECOND export const MILLISECONDS_A_DAY = SECONDS_A_DAY * MILLISECONDS_A_SECOND export const MILLISECONDS_A_WEEK = SECONDS_A_WEEK * MILLISECONDS_A_SECOND + +export const MS = 'millisecond' +export const S = 'second' +export const MIN = 'minute' +export const H = 'hour' +export const D = 'day' +export const W = 'week' +export const M = 'month' +export const Q = 'quarter' +export const Y = 'year' +export const DATE = 'date' diff --git a/src/index.js b/src/index.js index e120e7419..ab407ed2a 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ -import * as Constant from './constant' +import * as C from './constant' import * as Utils from './utils' const parseConfig = (config) => { @@ -70,19 +70,20 @@ class Dayjs { return this.$d.getTime() } - startOf(arg, isStartOf = true) { - switch (arg) { - case 'year': + startOf(units, isStartOf = true) { + const unit = Utils.prettyUnit(units) + switch (unit) { + case C.Y: if (isStartOf) { return new Dayjs(new Date(this.year(), 0, 1)) } return new Dayjs(new Date(this.year(), 11, 31)).endOf('day') - case 'month': + case C.M: if (isStartOf) { return new Dayjs(new Date(this.year(), this.month(), 1)) } return new Dayjs(new Date(this.year(), this.month() + 1, 0)).endOf('day') - case 'day': + case C.D: if (isStartOf) { return new Dayjs(this.toDate().setHours(0, 0, 0, 0)) } @@ -96,15 +97,16 @@ class Dayjs { return this.startOf(arg, false) } - mSet(string, int) { - switch (string) { - case 'date': + mSet(units, int) { + const unit = Utils.prettyUnit(units) + switch (unit) { + case C.DATE: this.$d.setDate(int) break - case 'month': + case C.M: this.$d.setMonth(int) break - case 'year': + case C.Y: this.$d.setFullYear(int) break default: @@ -119,32 +121,33 @@ class Dayjs { return this.clone().mSet(string, int) } - add(number, string) { - if (['M', 'months'].indexOf(string) > -1) { + add(number, units) { + const unit = (units && units.length === 1) ? units : Utils.prettyUnit(units) + if (['M', C.M].indexOf(unit) > -1) { let date = this.set('date', 1).set('month', this.month() + number) date = date.set('date', Math.min(this.date(), date.daysInMonth())) return date } let step - switch (string) { + switch (unit) { case 'm': - case 'minutes': - step = Constant.MILLISECONDS_A_MINUTE + case C.MIN: + step = C.MILLISECONDS_A_MINUTE break case 'h': - case 'hours': - step = Constant.MILLISECONDS_A_HOUR + case C.H: + step = C.MILLISECONDS_A_HOUR break case 'd': - case 'days': - step = Constant.MILLISECONDS_A_DAY + case C.D: + step = C.MILLISECONDS_A_DAY break case 'w': - case 'weeks': - step = Constant.MILLISECONDS_A_WEEK + case C.W: + step = C.MILLISECONDS_A_WEEK break default: // s seconds - step = Constant.MILLISECONDS_A_SECOND + step = C.MILLISECONDS_A_SECOND } const nextTimeStamp = this.valueOf() + (number * step) return new Dayjs(nextTimeStamp) @@ -195,27 +198,28 @@ class Dayjs { }) } - diff(input, unit, float = false) { + diff(input, units, float = false) { + const unit = Utils.prettyUnit(units) const that = input instanceof Dayjs ? input : new Dayjs(input) const diff = this - that let result = Utils.monthDiff(this, that) switch (unit) { - case 'years': + case C.Y: result /= 12 break - case 'months': + case C.M: break - case 'quarters': + case C.Q: result /= 3 break - case 'weeks': - result = diff / Constant.MILLISECONDS_A_WEEK + case C.W: + result = diff / C.MILLISECONDS_A_WEEK break - case 'days': - result = diff / Constant.MILLISECONDS_A_DAY + case C.D: + result = diff / C.MILLISECONDS_A_DAY break - case 'seconds': - result = diff / Constant.MILLISECONDS_A_SECOND + case C.S: + result = diff / C.MILLISECONDS_A_SECOND break default: // milliseconds result = diff diff --git a/src/utils.js b/src/utils.js index b78429ef4..37901154a 100644 --- a/src/utils.js +++ b/src/utils.js @@ -23,3 +23,4 @@ export const monthDiff = (a, b) => { export const absFloor = n => (n < 0 ? Math.ceil(n) || 0 : Math.floor(n)) +export const prettyUnit = u => (u && String(u).toLowerCase().replace(/s$/, '')) diff --git a/test/manipulate.test.js b/test/manipulate.test.js index a079b6b3d..fff7867a8 100644 --- a/test/manipulate.test.js +++ b/test/manipulate.test.js @@ -10,8 +10,8 @@ afterEach(() => { MockDate.reset() }) -it('StartOf EndOf Year', () => { - expect(dayjs().startOf('year').unix()).toBe(moment().startOf('year').unix()) +it('StartOf EndOf Year with s and upper case', () => { + expect(dayjs().startOf('YearS').unix()).toBe(moment().startOf('year').unix()) expect(dayjs().endOf('year').unix()).toBe(moment().endOf('year').unix()) }) From 21100390ea6620ed73dde207460efe9dc955d8c1 Mon Sep 17 00:00:00 2001 From: iamkun Date: Fri, 20 Apr 2018 10:20:36 +0800 Subject: [PATCH 08/12] fix(isValid): add isValid --- src/index.js | 4 ++++ test/parse.test.js | 6 ++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index ab407ed2a..4cbda118e 100644 --- a/src/index.js +++ b/src/index.js @@ -33,6 +33,10 @@ class Dayjs { this.$ms = this.$d.getMilliseconds() } + isValid() { + return !(this.$d.toString() === 'Invalid Date') + } + year() { return this.$y } diff --git a/test/parse.test.js b/test/parse.test.js index 0b26fd2e0..f8530c353 100644 --- a/test/parse.test.js +++ b/test/parse.test.js @@ -18,14 +18,16 @@ describe('Parse', () => { expect(dayjs('20130208').unix()).toBe(moment('20130208').unix()) }) - it('String ISO 8601 date, time and zone ', () => { + it('String ISO 8601 date, time and zone', () => { const time = '2018-04-04T16:00:00.000Z' expect(dayjs(time).unix()).toBe(moment(time).unix()) }) - it('String Other', () => { + it('String Other and isValid', () => { global.console.warn = jest.genMockFunction()// moment.js otherString will throw warn expect(dayjs('otherString').toString().toLowerCase()).toBe(moment('otherString').toString().toLowerCase()) + expect(dayjs().isValid()).toBe(true) + expect(dayjs('otherString').isValid()).toBe(false) }) }) From 1ad0b464dbc30c7baea62970a192fc4c874f9155 Mon Sep 17 00:00:00 2001 From: iamkun Date: Fri, 20 Apr 2018 10:37:09 +0800 Subject: [PATCH 09/12] fix(add year): add add year --- src/index.js | 9 ++++++--- test/manipulate.test.js | 1 + 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 4cbda118e..240d0838d 100644 --- a/src/index.js +++ b/src/index.js @@ -74,7 +74,7 @@ class Dayjs { return this.$d.getTime() } - startOf(units, isStartOf = true) { + startOf(units, isStartOf = true) { // isStartOf -> endOf const unit = Utils.prettyUnit(units) switch (unit) { case C.Y: @@ -128,10 +128,13 @@ class Dayjs { add(number, units) { const unit = (units && units.length === 1) ? units : Utils.prettyUnit(units) if (['M', C.M].indexOf(unit) > -1) { - let date = this.set('date', 1).set('month', this.month() + number) - date = date.set('date', Math.min(this.date(), date.daysInMonth())) + let date = this.set(C.DATE, 1).set(C.M, this.month() + number) + date = date.set(C.DATE, Math.min(this.date(), date.daysInMonth())) return date } + if (['y', C.Y].indexOf(unit) > -1) { + return this.set(C.Y, this.year() + number) + } let step switch (unit) { case 'm': diff --git a/test/manipulate.test.js b/test/manipulate.test.js index fff7867a8..1b4c9acb3 100644 --- a/test/manipulate.test.js +++ b/test/manipulate.test.js @@ -42,6 +42,7 @@ it('Add Time days', () => { expect(dayjs().add(1, 'd').unix()).toBe(moment().add(1, 'd').unix()) expect(dayjs().add(1, 'days').unix()).toBe(moment().add(1, 'days').unix()) expect(dayjs().add(1, 'M').unix()).toBe(moment().add(1, 'M').unix()) + expect(dayjs().add(1, 'y').unix()).toBe(moment().add(1, 'y').unix()) expect(dayjs('20111031').add(1, 'months').unix()).toBe(moment('20111031').add(1, 'months').unix()) }) From 871011c44cd69d10b0045f6fcafc10a5bf148484 Mon Sep 17 00:00:00 2001 From: iamkun Date: Fri, 20 Apr 2018 10:55:21 +0800 Subject: [PATCH 10/12] refactor(short): short --- src/index.js | 48 ++++++++++++++++++++++++-------------------- test/get-set.test.js | 16 +++++++++++++++ test/query.test.js | 16 +++++++++++++++ 3 files changed, 58 insertions(+), 22 deletions(-) create mode 100644 test/query.test.js diff --git a/src/index.js b/src/index.js index 240d0838d..f66ca29bf 100644 --- a/src/index.js +++ b/src/index.js @@ -37,6 +37,10 @@ class Dayjs { return !(this.$d.toString() === 'Invalid Date') } + isLeapYear() { + return ((this.$y % 4 === 0) && (this.$y % 100 !== 0)) || (this.$y % 400 === 0) + } + year() { return this.$y } @@ -79,14 +83,14 @@ class Dayjs { switch (unit) { case C.Y: if (isStartOf) { - return new Dayjs(new Date(this.year(), 0, 1)) + return new Dayjs(new Date(this.$y, 0, 1)) } - return new Dayjs(new Date(this.year(), 11, 31)).endOf('day') + return new Dayjs(new Date(this.$y, 11, 31)).endOf('day') case C.M: if (isStartOf) { - return new Dayjs(new Date(this.year(), this.month(), 1)) + return new Dayjs(new Date(this.$y, this.$M, 1)) } - return new Dayjs(new Date(this.year(), this.month() + 1, 0)).endOf('day') + return new Dayjs(new Date(this.$y, this.$M + 1, 0)).endOf('day') case C.D: if (isStartOf) { return new Dayjs(this.toDate().setHours(0, 0, 0, 0)) @@ -128,12 +132,12 @@ class Dayjs { add(number, units) { const unit = (units && units.length === 1) ? units : Utils.prettyUnit(units) if (['M', C.M].indexOf(unit) > -1) { - let date = this.set(C.DATE, 1).set(C.M, this.month() + number) - date = date.set(C.DATE, Math.min(this.date(), date.daysInMonth())) + 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 } if (['y', C.Y].indexOf(unit) > -1) { - return this.set(C.Y, this.year() + number) + return this.set(C.Y, this.$y + number) } let step switch (unit) { @@ -235,7 +239,7 @@ class Dayjs { } daysInMonth() { - return this.endOf('month').date() + return this.endOf(C.M).$D } clone() { @@ -248,13 +252,13 @@ class Dayjs { toArray() { return [ - this.year(), - this.month(), - this.date(), - this.hour(), - this.minute(), - this.second(), - this.millisecond() + this.$y, + this.$M, + this.$D, + this.$H, + this.$m, + this.$s, + this.$ms ] } @@ -268,13 +272,13 @@ class Dayjs { toObject() { return { - years: this.year(), - months: this.month(), - date: this.date(), - hours: this.hour(), - minutes: this.minute(), - seconds: this.second(), - milliseconds: this.millisecond() + years: this.$y, + months: this.$M, + date: this.$D, + hours: this.$H, + minutes: this.$m, + seconds: this.$s, + milliseconds: this.$ms } } diff --git a/test/get-set.test.js b/test/get-set.test.js index b09ae1acf..35a738650 100644 --- a/test/get-set.test.js +++ b/test/get-set.test.js @@ -22,6 +22,22 @@ it('Date', () => { expect(dayjs().date()).toBe(moment().date()) }) +it('Hour', () => { + expect(dayjs().hour()).toBe(moment().hour()) +}) + +it('Minute', () => { + expect(dayjs().minute()).toBe(moment().minute()) +}) + +it('Second', () => { + expect(dayjs().second()).toBe(moment().second()) +}) + +it('Millisecond', () => { + expect(dayjs().millisecond()).toBe(moment().millisecond()) +}) + it('Set Unknown String', () => { const newDate = dayjs().set('Unknown String', 1) expect(newDate.unix()) diff --git a/test/query.test.js b/test/query.test.js new file mode 100644 index 000000000..f2cbe050a --- /dev/null +++ b/test/query.test.js @@ -0,0 +1,16 @@ +// import moment from 'moment' +import MockDate from 'mockdate' +import dayjs from '../src' + +beforeEach(() => { + MockDate.set(new Date()) +}) + +afterEach(() => { + MockDate.reset() +}) + +it('IsLeapYear', () => { + expect(dayjs('20000101').isLeapYear()).toBe(true) + expect(dayjs('21000101').isLeapYear()).toBe(false) +}) From 5a0a6ffbf10761bd8b4b90298281265a0cebdbc2 Mon Sep 17 00:00:00 2001 From: iamkun Date: Fri, 20 Apr 2018 11:03:59 +0800 Subject: [PATCH 11/12] fix(add): add isBefore isAfter isSame --- src/index.js | 12 ++++++++++++ test/query.test.js | 13 ++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index f66ca29bf..5af04c3ec 100644 --- a/src/index.js +++ b/src/index.js @@ -41,6 +41,18 @@ class Dayjs { return ((this.$y % 4 === 0) && (this.$y % 100 !== 0)) || (this.$y % 400 === 0) } + isSame(that) { + return this.valueOf() === that.valueOf() + } + + isBefore(that) { + return this.valueOf() < that.valueOf() + } + + isAfter(that) { + return this.valueOf() > that.valueOf() + } + year() { return this.$y } diff --git a/test/query.test.js b/test/query.test.js index f2cbe050a..5b930541c 100644 --- a/test/query.test.js +++ b/test/query.test.js @@ -1,4 +1,4 @@ -// import moment from 'moment' +import moment from 'moment' import MockDate from 'mockdate' import dayjs from '../src' @@ -14,3 +14,14 @@ it('IsLeapYear', () => { expect(dayjs('20000101').isLeapYear()).toBe(true) expect(dayjs('21000101').isLeapYear()).toBe(false) }) + +it('Is Before Is After Is Same', () => { + [moment, dayjs].forEach((instance) => { + const dayA = instance() + const dayB = dayA.clone().add(1, 'day') + const dayC = dayA.clone().subtract(1, 'day') + expect(dayC.isBefore(dayA)).toBe(true) + expect(dayA.isSame(instance())).toBe(true) + expect(dayB.isAfter(dayA)).toBe(true) + }) +}) From af83c2609debf52f6e9aa25ea8d318b3337b8c1f Mon Sep 17 00:00:00 2001 From: iamkun Date: Fri, 20 Apr 2018 11:05:42 +0800 Subject: [PATCH 12/12] test: update test --- test/query.test.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/query.test.js b/test/query.test.js index 5b930541c..15a941b0c 100644 --- a/test/query.test.js +++ b/test/query.test.js @@ -10,13 +10,15 @@ afterEach(() => { MockDate.reset() }) +const testArr = [dayjs, moment] + it('IsLeapYear', () => { expect(dayjs('20000101').isLeapYear()).toBe(true) expect(dayjs('21000101').isLeapYear()).toBe(false) }) it('Is Before Is After Is Same', () => { - [moment, dayjs].forEach((instance) => { + testArr.forEach((instance) => { const dayA = instance() const dayB = dayA.clone().add(1, 'day') const dayC = dayA.clone().subtract(1, 'day')