Skip to content

Commit

Permalink
fix: Duration plugin supports parse ISO string with week (W) (#950)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun committed Jul 2, 2020
1 parent 81d4740 commit f0fc12a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/plugin/duration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Duration {
const d = input.match(durationRegex)
if (d) {
[,,
this.$d.years, this.$d.months,,
this.$d.years, this.$d.months, this.$d.weeks,
this.$d.days, this.$d.hours, this.$d.minutes, this.$d.seconds] = d
this.calMilliseconds()
return this
Expand Down Expand Up @@ -83,7 +83,7 @@ class Duration {
toISOString() {
const Y = this.$d.years ? `${this.$d.years}Y` : ''
const M = this.$d.months ? `${this.$d.months}M` : ''
let days = this.$d.days || 0
let days = +this.$d.days || 0
if (this.$d.weeks) {
days += this.$d.weeks * 7
}
Expand Down
6 changes: 6 additions & 0 deletions test/plugin/duration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ describe('Parse ISO string', () => {
it('Part ISO string', () => {
expect(dayjs.duration('PT2777H46M40S').toISOString()).toBe('PT2777H46M40S')
})
it('ISO string with week', () => {
const d = dayjs.duration('P2M3W4D')
expect(d.toISOString()).toBe('P2M25D')
expect(d.asDays()).toBe(85) // moment 85, count 2M as 61 days
expect(d.asWeeks()).toBe(12.142857142857142) // moment 12.285714285714286
})
it('Invalid ISO string', () => {
expect(dayjs.duration('Invalid').toISOString()).toBe('P0D')
})
Expand Down

0 comments on commit f0fc12a

Please sign in to comment.