]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Fix duration functions to follow tests
authorKunal Marwaha <marwahaha@berkeley.edu>
Mon, 5 Dec 2016 07:37:07 +0000 (02:37 -0500)
committerIskren Chernev <iskren.chernev@gmail.com>
Thu, 2 Mar 2017 08:45:50 +0000 (10:45 +0200)
src/lib/duration/as.js
src/lib/duration/get.js
src/lib/duration/iso-string.js
src/lib/duration/prototype.js
src/test/moment/duration.js
src/test/moment/duration_invalid.js

index 03ecd6dab2571180214facd47e16097eaa037673..122204a17d4dfa8c01cd7d0d4d11ef845229b7a6 100644 (file)
@@ -3,6 +3,9 @@ import { normalizeUnits } from '../units/aliases';
 import toInt from '../utils/to-int';
 
 export function as (units) {
+    if (!this.isValid()) {
+        return NaN;
+    }
     var days;
     var months;
     var milliseconds = this._milliseconds;
@@ -31,6 +34,9 @@ export function as (units) {
 
 // TODO: Use this.as('ms')?
 export function valueOf () {
+    if (!this.isValid()) {
+        return NaN;
+    }
     return (
         this._milliseconds +
         this._days * 864e5 +
index 6dafacdd8603d41c43c618783a60a52f0d9c7b52..8993e07443b886e9fef7bb7bed179b399ef99321 100644 (file)
@@ -3,12 +3,12 @@ import absFloor from '../utils/abs-floor';
 
 export function get (units) {
     units = normalizeUnits(units);
-    return this[units + 's']();
+    return this.isValid() ? this[units + 's']() : NaN;
 }
 
 function makeGetter(name) {
     return function () {
-        return this._data[name];
+        return this.isValid() ? this._data[name] : NaN;
     };
 }
 
index f33a968da8eebc8d1bd4e7277d502801e6187607..5a2be14e6e0d59c16bc0e257ed19806d458afcd9 100644 (file)
@@ -9,6 +9,10 @@ export function toISOString() {
     // This is because there is no context-free conversion between hours and days
     // (think of clock changes)
     // and also not between days and months (28-31 days per month)
+    if (!this.isValid()) {
+        return this.localeData().invalidDate();
+    }
+
     var seconds = abs(this._milliseconds) / 1000;
     var days         = abs(this._days);
     var months       = abs(this._months);
@@ -50,3 +54,8 @@ export function toISOString() {
         (m ? m + 'M' : '') +
         (s ? s + 'S' : '');
 }
+
+export function toJSON() {
+    // this may not be fully implemented
+    return this.isValid() ? this.toISOString() : null;
+}
index 0257ff9b501c84e3fba8aeebca204ba18df4168c..ef02a3145883e100b4abe6f2750dcf0dcadf4a80 100644 (file)
@@ -8,7 +8,7 @@ import { as, asMilliseconds, asSeconds, asMinutes, asHours, asDays, asWeeks, asM
 import { bubble } from './bubble';
 import { get, milliseconds, seconds, minutes, hours, days, months, years, weeks } from './get';
 import { humanize } from './humanize';
-import { toISOString } from './iso-string';
+import { toISOString, toJSON } from './iso-string';
 import { lang, locale, localeData } from '../moment/locale';
 
 proto.abs            = abs;
@@ -37,7 +37,7 @@ proto.years          = years;
 proto.humanize       = humanize;
 proto.toISOString    = toISOString;
 proto.toString       = toISOString;
-proto.toJSON         = toISOString;
+proto.toJSON         = toJSON;
 proto.locale         = locale;
 proto.localeData     = localeData;
 
index 70fe4a43ef39003f1a5b78bb975ccb43a87af245..dc57f21caaf82a83200abb8ede1d70cbfd6c58d8 100644 (file)
@@ -65,7 +65,7 @@ test('null instantiation', function (assert) {
 });
 
 test('NaN instantiation', function (assert) {
-    assert.equal(moment.duration(NaN).milliseconds(), 0, 'milliseconds');
+    assert.ok(isNaN(moment.duration(NaN).milliseconds()), 'milliseconds should be NaN');
     assert.equal(moment.duration(NaN).isValid(), false, '_isValid');
     assert.equal(moment.duration(NaN).humanize(), 'Invalid date', 'Duration should be invalid');
 });
index 2dbe9cc3cf7fef3c831e76202d4183781fd4895e..6ded11c957c78a399a2042c2d4b6689ee20c8a09 100644 (file)
@@ -3,7 +3,7 @@ import moment from '../../moment';
 
 module('invalid');
 
-test('invalid', function (assert) {
+test('invalid duration', function (assert) {
     var m = moment.duration(NaN); // should be invalid
     assert.equal(m.isValid(), false);
     assert.ok(isNaN(m.valueOf()));
@@ -15,7 +15,7 @@ test('invalid', function (assert) {
 //     assert.ok(isNaN(m.valueOf()));
 // });
 
-test('invalid operations', function (assert) {
+test('invalid duration operations', function (assert) {
     var invalids = [
             moment.duration(NaN)
             // moment.duration({invalidMonth : 'whatchamacallit'})