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: Duration add/subtract #2337

Merged
merged 3 commits into from
Jun 24, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 19 additions & 2 deletions src/plugin/duration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,15 @@ class Duration {
asYears() { return this.as('years') }
}

const manipulateDuration = (date, duration, k) =>
date.add(duration.years() * k, 'year')
.add(duration.months() * k, 'month')
.add(duration.days() * k, 'day')
.add(duration.hours() * k, 'hour')
.add(duration.minutes() * k, 'minute')
.add(duration.seconds() * k, 'second')
.add(duration.milliseconds() * k, 'millisecond')
Copy link
Owner

@iamkun iamkun Jun 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can replace 'second' with 's' according to https://github.com/iamkun/dayjs/blob/dev/src/utils.js#L31

to save some bytes

the rest LGTM


export default (option, Dayjs, dayjs) => {
$d = dayjs
$u = dayjs().$utils()
Expand All @@ -268,12 +277,20 @@ export default (option, Dayjs, dayjs) => {

const oldAdd = Dayjs.prototype.add
const oldSubtract = Dayjs.prototype.subtract

Dayjs.prototype.add = function (value, unit) {
if (isDuration(value)) value = value.asMilliseconds()
if (isDuration(value)) {
return manipulateDuration(this, value, 1)
}

return oldAdd.bind(this)(value, unit)
}

Dayjs.prototype.subtract = function (value, unit) {
if (isDuration(value)) value = value.asMilliseconds()
if (isDuration(value)) {
return manipulateDuration(this, value, -1)
}

return oldSubtract.bind(this)(value, unit)
}
}
12 changes: 12 additions & 0 deletions test/plugin/duration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ test('Add duration', () => {
expect(a.add(days).format('YYYY-MM-DD')).toBe('2020-10-03')
})

test('Add duration month', () => {
const a = dayjs('2023-02-01')
const days = dayjs.duration(1, 'month')
expect(a.add(days).format('YYYY-MM-DD')).toBe('2023-03-01')
})

describe('Subtract', () => {
const a = dayjs.duration(3, 'days')
const b = dayjs.duration(2, 'days')
Expand All @@ -196,6 +202,12 @@ test('Subtract duration', () => {
expect(a.subtract(days).format('YYYY-MM-DD')).toBe('2020-10-18')
})

test('Subtract duration month', () => {
const a = dayjs('2023-03-01')
const days = dayjs.duration(1, 'month')
expect(a.subtract(days).format('YYYY-MM-DD')).toBe('2023-02-01')
})

describe('Seconds', () => {
expect(dayjs.duration(500).seconds()).toBe(0)
expect(dayjs.duration(1500).seconds()).toBe(1)
Expand Down