Skip to content

Commit

Permalink
fix: iamkun#2336 combine add and subtract
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-ulaev committed Jun 23, 2023
1 parent 36bdbd0 commit b6a5a44
Showing 1 changed file with 10 additions and 19 deletions.
29 changes: 10 additions & 19 deletions src/plugin/duration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,23 +257,14 @@ class Duration {
asYears() { return this.as('years') }
}

const addDuration = (date, duration) =>
date.add(duration.years(), 'year')
.add(duration.months(), 'month')
.add(duration.days(), 'day')
.add(duration.hours(), 'hour')
.add(duration.minutes(), 'minute')
.add(duration.seconds(), 'second')
.add(duration.milliseconds(), 'millisecond')

const subtractDuration = (date, duration) =>
date.subtract(duration.years(), 'year')
.subtract(duration.months(), 'month')
.subtract(duration.days(), 'day')
.subtract(duration.hours(), 'hour')
.subtract(duration.minutes(), 'minute')
.subtract(duration.seconds(), 'second')
.subtract(duration.milliseconds(), 'millisecond')
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')

export default (option, Dayjs, dayjs) => {
$d = dayjs
Expand All @@ -289,15 +280,15 @@ export default (option, Dayjs, dayjs) => {

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

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

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

return oldSubtract.bind(this)(value, unit)
Expand Down

0 comments on commit b6a5a44

Please sign in to comment.