From: Kunal Marwaha Date: Mon, 5 Dec 2016 07:37:07 +0000 (-0500) Subject: Fix duration functions to follow tests X-Git-Tag: 2.18.0~45^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e3c482b2deedb8d9ca53b196082b21e8d532c5cb;p=thirdparty%2Fmoment.git Fix duration functions to follow tests --- diff --git a/src/lib/duration/as.js b/src/lib/duration/as.js index 03ecd6dab..122204a17 100644 --- a/src/lib/duration/as.js +++ b/src/lib/duration/as.js @@ -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 + diff --git a/src/lib/duration/get.js b/src/lib/duration/get.js index 6dafacdd8..8993e0744 100644 --- a/src/lib/duration/get.js +++ b/src/lib/duration/get.js @@ -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; }; } diff --git a/src/lib/duration/iso-string.js b/src/lib/duration/iso-string.js index f33a968da..5a2be14e6 100644 --- a/src/lib/duration/iso-string.js +++ b/src/lib/duration/iso-string.js @@ -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; +} diff --git a/src/lib/duration/prototype.js b/src/lib/duration/prototype.js index 0257ff9b5..ef02a3145 100644 --- a/src/lib/duration/prototype.js +++ b/src/lib/duration/prototype.js @@ -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; diff --git a/src/test/moment/duration.js b/src/test/moment/duration.js index 70fe4a43e..dc57f21ca 100644 --- a/src/test/moment/duration.js +++ b/src/test/moment/duration.js @@ -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'); }); diff --git a/src/test/moment/duration_invalid.js b/src/test/moment/duration_invalid.js index 2dbe9cc3c..6ded11c95 100644 --- a/src/test/moment/duration_invalid.js +++ b/src/test/moment/duration_invalid.js @@ -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'})