Skip to content

Commit

Permalink
fix: update adding/subtracting Duration from Dayjs object (#1156)
Browse files Browse the repository at this point in the history
  • Loading branch information
allmoviestvshowslistsfilmography28 committed Oct 21, 2020
1 parent 153d268 commit a481a16
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
14 changes: 8 additions & 6 deletions src/plugin/duration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,13 @@ 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()
units = 'ms'
}
return oldAdd.bind(this)(addition, units)
const oldSubtract = Dayjs.prototype.subtract
Dayjs.prototype.add = function (value, unit) {
if (isDuration(value)) value = value.asMilliseconds()
return oldAdd.bind(this)(value, unit)
}
Dayjs.prototype.subtract = function (value, unit) {
if (isDuration(value)) value = value.asMilliseconds()
return oldSubtract.bind(this)(value, unit)
}
}
7 changes: 6 additions & 1 deletion test/plugin/duration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ declare namespace dayjs {
*
* 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.
* ```
Expand All @@ -236,7 +236,7 @@ declare namespace dayjs {
*
* 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.
* ```
Expand Down
5 changes: 5 additions & 0 deletions types/plugin/duration.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit a481a16

Please sign in to comment.