From 8922b8f0b6c37bede752d3cd134ef5d1ab3e5a40 Mon Sep 17 00:00:00 2001 From: Mike Fogel Date: Tue, 20 Oct 2020 10:33:00 -0300 Subject: [PATCH 1/5] fix: add typescript typings for add/subtract of durations --- types/plugin/duration.d.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/types/plugin/duration.d.ts b/types/plugin/duration.d.ts index 3f6791523..c09dc9f09 100644 --- a/types/plugin/duration.d.ts +++ b/types/plugin/duration.d.ts @@ -56,6 +56,11 @@ declare namespace plugin { } declare module 'dayjs' { + interface Dayjs { + add(value: plugin.Duration): Dayjs + subtract(value: plugin.Duration): Dayjs + } + export function duration(input?: plugin.DurationInputType , unit?: string): plugin.Duration export function isDuration(d: any): d is plugin.Duration } From f810b61c48cb5c8b7d981a0f5ba6699cac8c8881 Mon Sep 17 00:00:00 2001 From: Mike Fogel Date: Tue, 20 Oct 2020 11:50:44 -0300 Subject: [PATCH 2/5] fix: support passing duration to subtract() --- src/plugin/duration/index.js | 15 +++++++++++---- test/plugin/duration.test.js | 7 ++++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/plugin/duration/index.js b/src/plugin/duration/index.js index 48ceadd8f..d2ec51fa7 100644 --- a/src/plugin/duration/index.js +++ b/src/plugin/duration/index.js @@ -179,11 +179,18 @@ export default (option, Dayjs, dayjs) => { dayjs.isDuration = isDuration const oldAdd = Dayjs.prototype.add - Dayjs.prototype.add = function (addition, units) { - if (isDuration(addition)) { - addition = addition.asMilliseconds() + const oldSubtract = Dayjs.prototype.subtract + const transformArgs = (value, units) => { + if (isDuration(value)) { + value = value.asMilliseconds() units = 'ms' } - return oldAdd.bind(this)(addition, units) + return [value, units] + } + Dayjs.prototype.add = function (value, units) { + return oldAdd.apply(this, transformArgs(value, units)) + } + Dayjs.prototype.subtract = function (value, units) { + return oldSubtract.apply(this, transformArgs(value, units)) } } diff --git a/test/plugin/duration.test.js b/test/plugin/duration.test.js index a85accf31..5e5447658 100644 --- a/test/plugin/duration.test.js +++ b/test/plugin/duration.test.js @@ -139,7 +139,7 @@ describe('Add', () => { expect(a.add({ days: 5 }).days()).toBe(6) }) -describe('Add duration', () => { +test('Add duration', () => { const a = dayjs('2020-10-01') const days = dayjs.duration(2, 'days') expect(a.add(days).format('YYYY-MM-DD')).toBe('2020-10-03') @@ -151,6 +151,11 @@ describe('Subtract', () => { expect(a.subtract(b).days()).toBe(1) }) +test('Subtract duration', () => { + const a = dayjs('2020-10-20') + const days = dayjs.duration(2, 'days') + expect(a.subtract(days).format('YYYY-MM-DD')).toBe('2020-10-18') +}) describe('Seconds', () => { expect(dayjs.duration(500).seconds()).toBe(0) From 26ee8f641734ee364c616ae9be873600130afcfc Mon Sep 17 00:00:00 2001 From: Mike Fogel Date: Wed, 21 Oct 2020 07:56:37 -0300 Subject: [PATCH 3/5] refactor: simplify argument transformation --- src/plugin/duration/index.js | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/plugin/duration/index.js b/src/plugin/duration/index.js index d2ec51fa7..50f660484 100644 --- a/src/plugin/duration/index.js +++ b/src/plugin/duration/index.js @@ -180,17 +180,12 @@ export default (option, Dayjs, dayjs) => { const oldAdd = Dayjs.prototype.add const oldSubtract = Dayjs.prototype.subtract - const transformArgs = (value, units) => { - if (isDuration(value)) { - value = value.asMilliseconds() - units = 'ms' - } - return [value, units] - } - Dayjs.prototype.add = function (value, units) { - return oldAdd.apply(this, transformArgs(value, units)) + Dayjs.prototype.add = function (value, unit) { + if (isDuration(value)) value = value.asMilliseconds() + return oldAdd.bind(this)(value, unit) } - Dayjs.prototype.subtract = function (value, units) { - return oldSubtract.apply(this, transformArgs(value, units)) + Dayjs.prototype.subtract = function (value, unit) { + if (isDuration(value)) value = value.asMilliseconds() + return oldSubtract.bind(this)(value, unit) } } From 379f3484fca94057f2f39d26e503aa83b031fbc2 Mon Sep 17 00:00:00 2001 From: Mike Fogel Date: Wed, 21 Oct 2020 08:01:41 -0300 Subject: [PATCH 4/5] fix: unit is an optional arg for add() and subtract() --- types/index.d.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index e3e62495d..5ad15cc91 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -222,21 +222,21 @@ declare namespace dayjs { * ``` * dayjs().add(7, 'day')// => Dayjs * ``` - * Units are case insensitive, and support plural and short forms. + * Units are case insensitive, support plural and short forms, and default to 'ms' if not supplied. * * Docs: https://day.js.org/docs/en/manipulate/add */ - add(value: number, unit: OpUnitType): Dayjs + add(value: number, unit?: OpUnitType): Dayjs /** * Returns a cloned Day.js object with a specified amount of time subtracted. * ``` * dayjs().subtract(7, 'year')// => Dayjs * ``` - * Units are case insensitive, and support plural and short forms. + * Units are case insensitive, support plural and short forms, and default to 'ms' if not supplied. * * Docs: https://day.js.org/docs/en/manipulate/subtract */ - subtract(value: number, unit: OpUnitType): Dayjs + subtract(value: number, unit?: OpUnitType): Dayjs /** * Returns a cloned Day.js object and set it to the start of a unit of time. * ``` From d3e94af6e28828144638125a623e6e7c2886f4fd Mon Sep 17 00:00:00 2001 From: Mike Fogel Date: Wed, 21 Oct 2020 10:14:34 -0300 Subject: [PATCH 5/5] fix: revert types docs to not mentioning default value --- types/index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index 5ad15cc91..fcc9f4c45 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -222,7 +222,7 @@ declare namespace dayjs { * ``` * dayjs().add(7, 'day')// => Dayjs * ``` - * Units are case insensitive, support plural and short forms, and default to 'ms' if not supplied. + * Units are case insensitive, and support plural and short forms. * * Docs: https://day.js.org/docs/en/manipulate/add */ @@ -232,7 +232,7 @@ declare namespace dayjs { * ``` * dayjs().subtract(7, 'year')// => Dayjs * ``` - * Units are case insensitive, support plural and short forms, and default to 'ms' if not supplied. + * Units are case insensitive, and support plural and short forms. * * Docs: https://day.js.org/docs/en/manipulate/subtract */