]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
valueOf and asMilliseconds have the same function
authorAlanscut <wp_scut@163.com>
Fri, 19 Jun 2020 01:37:19 +0000 (09:37 +0800)
committerIskren Chernev <me@iskren.info>
Tue, 26 Dec 2023 14:05:24 +0000 (16:05 +0200)
src/lib/duration/as.js
src/test/moment/duration.js

index ff9e67b009f9eada37a54bcc4fff85c9eff61fb7..bfb12dc86469750a5b74f64dda09686958968326 100644 (file)
@@ -1,6 +1,5 @@
 import { daysToMonths, monthsToDays } from './bubble';
 import { normalizeUnits } from '../units/aliases';
-import toInt from '../utils/to-int';
 
 export function as(units) {
     if (!this.isValid()) {
@@ -46,19 +45,6 @@ export function as(units) {
     }
 }
 
-// TODO: Use this.as('ms')?
-export function valueOf() {
-    if (!this.isValid()) {
-        return NaN;
-    }
-    return (
-        this._milliseconds +
-        this._days * 864e5 +
-        (this._months % 12) * 2592e6 +
-        toInt(this._months / 12) * 31536e6
-    );
-}
-
 function makeAs(alias) {
     return function () {
         return this.as(alias);
@@ -73,7 +59,8 @@ var asMilliseconds = makeAs('ms'),
     asWeeks = makeAs('w'),
     asMonths = makeAs('M'),
     asQuarters = makeAs('Q'),
-    asYears = makeAs('y');
+    asYears = makeAs('y'),
+    valueOf = makeAs('ms');
 
 export {
     asMilliseconds,
@@ -85,4 +72,5 @@ export {
     asMonths,
     asQuarters,
     asYears,
+    valueOf,
 };
index 679371b6bf778ca3eb7417f7a3d0995ffb2ab18b..5b832f4928b8a80551a30cf0461d31f51502b5a7 100644 (file)
@@ -2031,3 +2031,9 @@ test('duration plugins', function (assert) {
     };
     durationObject.foo(5);
 });
+
+test('valueOf and asMilliseconds have the same function', function (assert) {
+    var t1 = +moment.duration({ months: 2 }),
+        t2 = moment.duration({ months: 2 }).asMilliseconds();
+    assert.ok(t1 === t2, 'the final value should be equal');
+});