iamkun / iamkun/dayjs

Invalid duration when using partial object parameter in the creator

Open
#2,633 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
48.7k
Forks
2.5k
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**
The Duration creator returns incomplete duration if passed the object parameter that's partial:
![image](https://github.com/iamkun/dayjs/assets/6209244/cb23f643-4b35-452a-8b5d-2c2e4c97fea2)
![image](https://github.com/iamkun/dayjs/assets/6209244/e7f883b9-180d-498d-8c22-037f2a239546)

**Solution**
Adding `this.parseFromMilliseconds()` [in the constructor for the code branch handling the object parameter](https://github.com/iamkun/dayjs/blob/2076da970047c6e0a22c8d4243a42d52833a5df2/src/plugin/duration/index.js#L80) solves it:
```diff
if (typeof input === 'object') {
Object.keys(input).forEach((k) => {
this.$d[prettyUnit(k)] = input[k]
})
this.calMilliseconds()
+ this.parseFromMilliseconds()
return this
}
```

**Information**
- Day.js Version: 1.11.10

Contributor guide

Open the contributing guide

Research direction

Start in src/plugin/duration/index.js at the object-parameter branch identified in the issue, and reproduce the incomplete result with a partial duration object. Verify that the duration's component values and millisecond total are consistent after construction; the issue's proposed behavior provides the completion check.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.