diff --git a/src/plugin/isToday/index.js b/src/plugin/isToday/index.js new file mode 100644 index 000000000..55f0d8139 --- /dev/null +++ b/src/plugin/isToday/index.js @@ -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 + } +} diff --git a/src/plugin/isTomorrow/index.js b/src/plugin/isTomorrow/index.js new file mode 100644 index 000000000..62311ab37 --- /dev/null +++ b/src/plugin/isTomorrow/index.js @@ -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 + } +} + diff --git a/src/plugin/isYesterday/index.js b/src/plugin/isYesterday/index.js new file mode 100644 index 000000000..ced6d02a8 --- /dev/null +++ b/src/plugin/isYesterday/index.js @@ -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 + } +} + diff --git a/test/plugin/isToday.test.js b/test/plugin/isToday.test.js new file mode 100644 index 000000000..0e57a707a --- /dev/null +++ b/test/plugin/isToday.test.js @@ -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() +}) diff --git a/test/plugin/isTomorrow.test.js b/test/plugin/isTomorrow.test.js new file mode 100644 index 000000000..bd73d77e7 --- /dev/null +++ b/test/plugin/isTomorrow.test.js @@ -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() +}) diff --git a/test/plugin/isYesterday.test.js b/test/plugin/isYesterday.test.js new file mode 100644 index 000000000..ffb9a57e0 --- /dev/null +++ b/test/plugin/isYesterday.test.js @@ -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() +})