Skip to content

Commit

Permalink
Merge 8484045 into 3e1a95b
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Wang committed Jun 19, 2020
2 parents 3e1a95b + 8484045 commit 26e72d1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
18 changes: 3 additions & 15 deletions src/lib/duration/as.js
Original file line number Diff line number Diff line change
@@ -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()) {
Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand All @@ -85,4 +72,5 @@ export {
asMonths,
asQuarters,
asYears,
valueOf,
};
6 changes: 6 additions & 0 deletions src/test/moment/duration.js
Original file line number Diff line number Diff line change
Expand Up @@ -2022,3 +2022,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');
});

0 comments on commit 26e72d1

Please sign in to comment.