Skip to content

Commit

Permalink
fix: when parsing duration from ISO string, set missing components to…
Browse files Browse the repository at this point in the history
… 0 instead of NaN
  • Loading branch information
Kirill Rakhman committed Aug 18, 2021
1 parent f68e4b1 commit f92b47f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/plugin/duration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Duration {
const d = input.match(durationRegex)
if (d) {
const properties = d.slice(2)
const numberD = properties.map(value => Number(value));
const numberD = properties.map(value => (value != null ? Number(value) : 0));
[
this.$d.years,
this.$d.months,
Expand Down
3 changes: 3 additions & 0 deletions test/plugin/duration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ describe('Parse ISO string', () => {
it('Part ISO string', () => {
expect(dayjs.duration('PT2777H46M40S').toISOString()).toBe('PT2777H46M40S')
})
it('Formatting missing components', () => {
expect(dayjs.duration('PT1H').format('YYYY-MM-DDTHH:mm:ss')).toBe('0000-00-00T01:00:00')
})
it('ISO string with week', () => {
const d = dayjs.duration('P2M3W4D')
expect(d.toISOString()).toBe('P2M25D')
Expand Down

0 comments on commit f92b47f

Please sign in to comment.