Skip to content

Commit

Permalink
feat: add relative date helpers plugins (iamkun#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
lamartire committed Apr 7, 2020
1 parent c39fb96 commit 77e1f30
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/plugin/isToday/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default (o, c, d) => {
const proto = c.prototype
proto.isToday = function () {
const comparisonKey = ['$y', '$D', '$m']
const now = d()

return comparisonKey.filter(function (key) {
return now[key] !== this[key]
}, this).length === 0
}
}
12 changes: 12 additions & 0 deletions src/plugin/isTomorrow/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default (o, c, d) => {
const proto = c.prototype
proto.isTomorrow = function () {
const comparisonKey = ['$y', '$D', '$m']
const tomorrow = d().add(1, 'day')

return comparisonKey.filter(function (key) {
return tomorrow[key] === this[key]
}, this).length === 3
}
}

12 changes: 12 additions & 0 deletions src/plugin/isYesterday/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default (o, c, d) => {
const proto = c.prototype
proto.isYesterday = function () {
const comparisonKey = ['$y', '$D', '$m']
const yesterday = d().subtract(1, 'day')

return comparisonKey.filter(function (key) {
return yesterday[key] === this[key]
}, this).length === 3
}
}

18 changes: 18 additions & 0 deletions test/plugin/isToday.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import MockDate from 'mockdate'
import dayjs from '../../src'
import isToday from '../../src/plugin/isToday'

dayjs.extend(isToday)

beforeEach(() => {
MockDate.set(new Date())
})

afterEach(() => {
MockDate.reset()
})

it('is today', () => {
expect(dayjs(new Date()).isToday()).toBeTruthy()
expect(dayjs('2017-01-01').isToday()).toBeFalsy()
})
18 changes: 18 additions & 0 deletions test/plugin/isTomorrow.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import MockDate from 'mockdate'
import dayjs from '../../src'
import isTomorrow from '../../src/plugin/isTomorrow'

dayjs.extend(isTomorrow)

beforeEach(() => {
MockDate.set(new Date())
})

afterEach(() => {
MockDate.reset()
})

it('is tomorrow', () => {
expect(dayjs().add(1, 'day').isTomorrow()).toBeTruthy()
expect(dayjs('2017-01-01').isTomorrow('2019-01-01', '2017-01-01')).toBeFalsy()
})
18 changes: 18 additions & 0 deletions test/plugin/isYesterday.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import MockDate from 'mockdate'
import dayjs from '../../src'
import isYesterday from '../../src/plugin/isYesterday'

dayjs.extend(isYesterday)

beforeEach(() => {
MockDate.set(new Date())
})

afterEach(() => {
MockDate.reset()
})

it('is yesterday', () => {
expect(dayjs().subtract(1, 'day').isYesterday()).toBeTruthy()
expect(dayjs('2017-01-01').isYesterday()).toBeFalsy()
})

0 comments on commit 77e1f30

Please sign in to comment.