Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isToday, isTomorrow, isYesterday plugins #857

Merged
merged 3 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']
epszaw marked this conversation as resolved.
Show resolved Hide resolved
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()
})
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
}
}