From a182b1fa0cbda76d25ef75333acfb0c11c193cf3 Mon Sep 17 00:00:00 2001 From: iamkun Date: Thu, 31 May 2018 11:16:43 +0800 Subject: [PATCH] refactor: update compare API --- src/index.js | 21 +++++++++++---------- test/query.test.js | 41 +++++++++++++++++++++++++++++++++-------- 2 files changed, 44 insertions(+), 18 deletions(-) diff --git a/src/index.js b/src/index.js index 8fb328eed..38f903f47 100644 --- a/src/index.js +++ b/src/index.js @@ -36,14 +36,11 @@ const dayjs = (date, c) => { return new Dayjs(cfg) // eslint-disable-line no-use-before-define } -const toDayjs = input => (isDayjs(input) ? input : dayjs(input.valueOf())) - const wrapper = (date, instance) => dayjs(date, { locale: instance.$L }) const Utils = U // for plugin use Utils.parseLocale = parseLocale Utils.isDayjs = isDayjs -Utils.toDayjs = toDayjs Utils.wrapper = wrapper const parseDate = (date) => { @@ -99,16 +96,20 @@ class Dayjs { return ((this.$y % 4 === 0) && (this.$y % 100 !== 0)) || (this.$y % 400 === 0) } - isSame(input) { - return this.valueOf() === toDayjs(input).valueOf() + $compare(that) { + return this.valueOf() - dayjs(that).valueOf() + } + + isSame(that) { + return this.$compare(that) === 0 } - isBefore(input) { - return this.valueOf() < toDayjs(input).valueOf() + isBefore(that) { + return this.$compare(that) < 0 } - isAfter(input) { - return this.valueOf() > toDayjs(input).valueOf() + isAfter(that) { + return this.$compare(that) > 0 } year() { @@ -339,7 +340,7 @@ class Dayjs { diff(input, units, float) { const unit = Utils.prettyUnit(units) - const that = toDayjs(input) + const that = dayjs(input) const diff = this - that let result = Utils.monthDiff(this, that) switch (unit) { diff --git a/test/query.test.js b/test/query.test.js index cf864dcc2..977a68e09 100644 --- a/test/query.test.js +++ b/test/query.test.js @@ -17,13 +17,38 @@ it('IsLeapYear', () => { expect(dayjs('2100-01-01').isLeapYear()).toBe(false) }) -it('Is Before Is After Is Same', () => { - testArr.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) +describe('Is Before Is After Is Same', () => { + it('Compare to dayjs object', () => { + testArr.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) + expect(dayA.isSame()).toBe(true) + expect(dayB.isAfter()).toBe(true) + expect(dayC.isBefore()).toBe(true) + }) + }) + + it('No value', () => { + testArr.forEach((instance) => { + const dayA = instance() + const dayB = dayA.clone().add(1, 'day') + const dayC = dayA.clone().subtract(1, 'day') + expect(dayA.isSame()).toBe(true) + expect(dayB.isAfter()).toBe(true) + expect(dayC.isBefore()).toBe(true) + }) + }) + + it('With string', () => { + testArr.forEach((instance) => { + const dayD = instance() + expect(dayD.isSame('20180101')).toBe(false) + expect(dayD.isAfter('20180101')).toBe(true) + expect(dayD.isBefore('20180101')).toBe(false) + }) }) })