Repository: iamkun/dayjs
Pull request #2226 (2023-02-04 08:55:19)

Added valueOf method to Duration class
The Duration class in MomentJS contains the valueOf method (https://github.com/moment/moment/blob/develop/src/lib/duration/as.js). This method returns the duration in milliseconds. This PR adds this method also to the Dayjs Duration class.

codecov[bot]: # [Codecov](https://codecov.io/gh/iamkun/dayjs/pull/2226?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#2226](https://codecov.io/gh/iamkun/dayjs/pull/2226?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (f49c37a) into [dev](https://codecov.io/gh/iamkun/dayjs/commit/7151139f047a71c39cf63608c6aeb4f6876c8078?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (7151139) will **not change** coverage.
> The diff coverage is `100.00%`.

> :exclamation: Current head f49c37a differs from pull request most recent head e5f13e1. Consider uploading reports for the commit e5f13e1 to get more accurate results

```diff
@@            Coverage Diff            @@
##               dev     #2226   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          181       183    +2     
  Lines         2074      2111   +37     
  Branches       544       555   +11     
=========================================
+ Hits          2074      2111   +37     
```


| [Impacted Files](https://codecov.io/gh/iamkun/dayjs/pull/2226?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [src/locale/fa.js](https://codecov.io/gh/iamkun/dayjs/pull/2226?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2xvY2FsZS9mYS5qcw==) | `100.00% <ø> (ø)` | |
| [src/locale/en.js](https://codecov.io/gh/iamkun/dayjs/pull/2226?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2xvY2FsZS9lbi5qcw==) | `100.00% <100.00%> (ø)` | |
| [src/locale/fr.js](https://codecov.io/gh/iamkun/dayjs/pull/2226?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2xvY2FsZS9mci5qcw==) | `100.00% <100.00%> (ø)` | |
| [src/locale/nl.js](https://codecov.io/gh/iamkun/dayjs/pull/2226?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2xvY2FsZS9ubC5qcw==) | `100.00% <100.00%> (ø)` | |
| [src/locale/zh-tw.js](https://codecov.io/gh/iamkun/dayjs/pull/2226?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2xvY2FsZS96aC10dy5qcw==) | `100.00% <100.00%> (ø)` | |
| [src/plugin/advancedFormat/index.js](https://codecov.io/gh/iamkun/dayjs/pull/2226?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL3BsdWdpbi9hZHZhbmNlZEZvcm1hdC9pbmRleC5qcw==) | `100.00% <100.00%> (ø)` | |
| [src/plugin/bigIntSupport/index.js](https://codecov.io/gh/iamkun/dayjs/pull/2226?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL3BsdWdpbi9iaWdJbnRTdXBwb3J0L2luZGV4Lmpz) | `100.00% <100.00%> (ø)` | |
| [src/plugin/duration/index.js](https://codecov.io/gh/iamkun/dayjs/pull/2226?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL3BsdWdpbi9kdXJhdGlvbi9pbmRleC5qcw==) | `100.00% <100.00%> (ø)` | |
| [src/plugin/objectSupport/index.js](https://codecov.io/gh/iamkun/dayjs/pull/2226?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL3BsdWdpbi9vYmplY3RTdXBwb3J0L2luZGV4Lmpz) | `100.00% <100.00%> (ø)` | |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)


Patch:
diff --git a/src/plugin/duration/index.js b/src/plugin/duration/index.js
index 10ebb4c..5d4cc6a 100644
--- a/src/plugin/duration/index.js
+++ b/src/plugin/duration/index.js
@@ -235,6 +235,10 @@ class Duration {
       .fromNow(!withSuffix)
   }
 
+  valueOf() {
+    return this.asMilliseconds()
+  }
+
   milliseconds() { return this.get('milliseconds') }
   asMilliseconds() { return this.as('milliseconds') }
   seconds() { return this.get('seconds') }


Test patch:
diff --git a/test/plugin/duration.test.js b/test/plugin/duration.test.js
index 2e80469..8a7e0e6 100644
--- a/test/plugin/duration.test.js
+++ b/test/plugin/duration.test.js
@@ -67,6 +67,9 @@ describe('Creating', () => {
       ms: -1
     }).toISOString()).toBe('-PT0.001S')
   })
+  it('convert to milliseconds', () => {
+    expect(+dayjs.duration(100)).toBe(100)
+  })
 })
 
 describe('Parse ISO string', () => {