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

Fix issues with adding/subtracting durations from Dayjs objects #1156

Merged
merged 5 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
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
}