From 77e1f300d54bb473eeb7795072fbbe3c861f7ca6 Mon Sep 17 00:00:00 2001 From: lamartire Date: Mon, 10 Sep 2018 23:53:08 +0300 Subject: [PATCH 1/3] feat: add relative date helpers plugins (#299) --- src/plugin/isToday/index.js | 11 +++++++++++ src/plugin/isTomorrow/index.js | 12 ++++++++++++ src/plugin/isYesterday/index.js | 12 ++++++++++++ test/plugin/isToday.test.js | 18 ++++++++++++++++++ test/plugin/isTomorrow.test.js | 18 ++++++++++++++++++ test/plugin/isYesterday.test.js | 18 ++++++++++++++++++ 6 files changed, 89 insertions(+) create mode 100644 src/plugin/isToday/index.js create mode 100644 src/plugin/isTomorrow/index.js create mode 100644 src/plugin/isYesterday/index.js create mode 100644 test/plugin/isToday.test.js create mode 100644 test/plugin/isTomorrow.test.js create mode 100644 test/plugin/isYesterday.test.js 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() +}) From af6128414bcedcaedd73c3642164704797d1a9b4 Mon Sep 17 00:00:00 2001 From: lamartire Date: Tue, 7 Apr 2020 17:01:34 +0300 Subject: [PATCH 2/3] feat: add type declarations for relative time plugins --- types/plugin/isToday.d.ts | 10 ++++++++++ types/plugin/isTomorrow.d.ts | 10 ++++++++++ types/plugin/isYesterday.d.ts | 10 ++++++++++ 3 files changed, 30 insertions(+) create mode 100644 types/plugin/isToday.d.ts create mode 100644 types/plugin/isTomorrow.d.ts create mode 100644 types/plugin/isYesterday.d.ts diff --git a/types/plugin/isToday.d.ts b/types/plugin/isToday.d.ts new file mode 100644 index 000000000..04ac5818d --- /dev/null +++ b/types/plugin/isToday.d.ts @@ -0,0 +1,10 @@ +import { PluginFunc } from 'dayjs' + +declare const plugin: PluginFunc +export = plugin + +declare module 'dayjs' { + interface Dayjs { + isToday(): boolean + } +} diff --git a/types/plugin/isTomorrow.d.ts b/types/plugin/isTomorrow.d.ts new file mode 100644 index 000000000..08110b6e2 --- /dev/null +++ b/types/plugin/isTomorrow.d.ts @@ -0,0 +1,10 @@ +import { PluginFunc } from 'dayjs' + +declare const plugin: PluginFunc +export = plugin + +declare module 'dayjs' { + interface Dayjs { + isTomorrow(): boolean + } +} diff --git a/types/plugin/isYesterday.d.ts b/types/plugin/isYesterday.d.ts new file mode 100644 index 000000000..2d8ae9e1a --- /dev/null +++ b/types/plugin/isYesterday.d.ts @@ -0,0 +1,10 @@ +import { PluginFunc } from 'dayjs' + +declare const plugin: PluginFunc +export = plugin + +declare module 'dayjs' { + interface Dayjs { + isYesterday(): boolean + } +} From bdf6a546712d102ae74297a99be30544e74bf081 Mon Sep 17 00:00:00 2001 From: lamartire Date: Wed, 8 Apr 2020 10:53:56 +0300 Subject: [PATCH 3/3] feat: change comprasion algorithm for relative time plugins --- src/plugin/isToday/index.js | 6 ++---- src/plugin/isTomorrow/index.js | 9 ++++----- src/plugin/isYesterday/index.js | 9 ++++----- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/plugin/isToday/index.js b/src/plugin/isToday/index.js index 55f0d8139..fe97a8b6f 100644 --- a/src/plugin/isToday/index.js +++ b/src/plugin/isToday/index.js @@ -1,11 +1,9 @@ export default (o, c, d) => { const proto = c.prototype proto.isToday = function () { - const comparisonKey = ['$y', '$D', '$m'] + const comparisonTemplate = 'YYYY-MM-DD' const now = d() - return comparisonKey.filter(function (key) { - return now[key] !== this[key] - }, this).length === 0 + return this.format(comparisonTemplate) === now.format(comparisonTemplate) } } diff --git a/src/plugin/isTomorrow/index.js b/src/plugin/isTomorrow/index.js index 62311ab37..f117766d0 100644 --- a/src/plugin/isTomorrow/index.js +++ b/src/plugin/isTomorrow/index.js @@ -1,12 +1,11 @@ export default (o, c, d) => { const proto = c.prototype proto.isTomorrow = function () { - const comparisonKey = ['$y', '$D', '$m'] + const comparisonTemplate = 'YYYY-MM-DD' const tomorrow = d().add(1, 'day') - return comparisonKey.filter(function (key) { - return tomorrow[key] === this[key] - }, this).length === 3 + return ( + this.format(comparisonTemplate) === tomorrow.format(comparisonTemplate) + ) } } - diff --git a/src/plugin/isYesterday/index.js b/src/plugin/isYesterday/index.js index ced6d02a8..aac904b28 100644 --- a/src/plugin/isYesterday/index.js +++ b/src/plugin/isYesterday/index.js @@ -1,12 +1,11 @@ export default (o, c, d) => { const proto = c.prototype proto.isYesterday = function () { - const comparisonKey = ['$y', '$D', '$m'] + const comparisonTemplate = 'YYYY-MM-DD' const yesterday = d().subtract(1, 'day') - return comparisonKey.filter(function (key) { - return yesterday[key] === this[key] - }, this).length === 3 + return ( + this.format(comparisonTemplate) === yesterday.format(comparisonTemplate) + ) } } -