Skip to content

Commit

Permalink
fix: dayjs add inconsistency with weeks
Browse files Browse the repository at this point in the history
  • Loading branch information
github-roushan committed Jan 17, 2025
1 parent 53dbe26 commit 956a9f6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
11 changes: 2 additions & 9 deletions src/plugin/duration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,7 @@ class Duration {
}

get(unit) {
let base = this.$ms
const pUnit = prettyUnit(unit)
if (pUnit === 'milliseconds') {
base %= 1000
} else if (pUnit === 'weeks') {
base = roundNumber(base / unitToMS[pUnit])
} else {
base = this.$d[pUnit]
}
const base = this.$d[`${prettyUnit(unit)}`]
return base || 0 // a === 0 will be true on both 0 and -0
}

Expand Down Expand Up @@ -266,6 +258,7 @@ const manipulateDuration = (date, duration, k) =>
.add(duration.minutes() * k, 'm')
.add(duration.seconds() * k, 's')
.add(duration.milliseconds() * k, 'ms')
.add(duration.weeks() * k, 'w')

export default (option, Dayjs, dayjs) => {
$d = dayjs
Expand Down
5 changes: 3 additions & 2 deletions test/plugin/duration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ describe('Days', () => {
})

describe('Weeks', () => {
expect(dayjs.duration(1000000000).weeks()).toBe(1)
expect(dayjs.duration(1000000000).weeks()).toBe(0)
expect(dayjs.duration(1000000000).asWeeks().toFixed(2)).toBe('1.65')
})

Expand Down Expand Up @@ -294,7 +294,8 @@ describe('Format', () => {
.add(16, 'days')
.add(10, 'months')
.add(22, 'years')
expect(d.format()).toBe('0022-10-16T13:35:15')
.add(1, "weeks")
expect(d.format()).toBe('0022-10-23T13:35:15')
})

test('with formatStr for all tokens', () => {
Expand Down

0 comments on commit 956a9f6

Please sign in to comment.