Skip to content

Commit

Permalink
Merge pull request #213 from iamkun/feature/iamkun
Browse files Browse the repository at this point in the history
refactor: update compare API
  • Loading branch information
iamkun authored May 31, 2018
2 parents cebc757 + a182b1f commit 2f9034e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
21 changes: 11 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand Down
41 changes: 33 additions & 8 deletions test/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})

0 comments on commit 2f9034e

Please sign in to comment.