Skip to content

Commit

Permalink
fix: add isToday, isTomorrow, isYesterday plugins (#857)
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Epishev authored Apr 8, 2020
1 parent d568273 commit fc08ab6
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/plugin/isToday/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default (o, c, d) => {
const proto = c.prototype
proto.isToday = function () {
const comparisonTemplate = 'YYYY-MM-DD'
const now = d()

return this.format(comparisonTemplate) === now.format(comparisonTemplate)
}
}
11 changes: 11 additions & 0 deletions src/plugin/isTomorrow/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.isTomorrow = function () {
const comparisonTemplate = 'YYYY-MM-DD'
const tomorrow = d().add(1, 'day')

return (
this.format(comparisonTemplate) === tomorrow.format(comparisonTemplate)
)
}
}
11 changes: 11 additions & 0 deletions src/plugin/isYesterday/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.isYesterday = function () {
const comparisonTemplate = 'YYYY-MM-DD'
const yesterday = d().subtract(1, 'day')

return (
this.format(comparisonTemplate) === yesterday.format(comparisonTemplate)
)
}
}
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()
})
10 changes: 10 additions & 0 deletions types/plugin/isToday.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { PluginFunc } from 'dayjs'

declare const plugin: PluginFunc
export = plugin

declare module 'dayjs' {
interface Dayjs {
isToday(): boolean
}
}
10 changes: 10 additions & 0 deletions types/plugin/isTomorrow.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { PluginFunc } from 'dayjs'

declare const plugin: PluginFunc
export = plugin

declare module 'dayjs' {
interface Dayjs {
isTomorrow(): boolean
}
}
10 changes: 10 additions & 0 deletions types/plugin/isYesterday.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { PluginFunc } from 'dayjs'

declare const plugin: PluginFunc
export = plugin

declare module 'dayjs' {
interface Dayjs {
isYesterday(): boolean
}
}

0 comments on commit fc08ab6

Please sign in to comment.