From f0fc12adadcab53fb0577ad8f5e2f1cf784fd8f5 Mon Sep 17 00:00:00 2001 From: iamkun Date: Thu, 2 Jul 2020 16:56:31 +0800 Subject: [PATCH] fix: Duration plugin supports parse ISO string with week (W) (#950) --- src/plugin/duration/index.js | 4 ++-- test/plugin/duration.test.js | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/plugin/duration/index.js b/src/plugin/duration/index.js index 28031b127..31ab68572 100644 --- a/src/plugin/duration/index.js +++ b/src/plugin/duration/index.js @@ -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 @@ -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 } diff --git a/test/plugin/duration.test.js b/test/plugin/duration.test.js index e854d9148..d2e4abe6c 100644 --- a/test/plugin/duration.test.js +++ b/test/plugin/duration.test.js @@ -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') })