import toInt from '../utils/to-int';
export function as (units) {
+ if (!this.isValid()) {
+ return NaN;
+ }
var days;
var months;
var milliseconds = this._milliseconds;
// TODO: Use this.as('ms')?
export function valueOf () {
+ if (!this.isValid()) {
+ return NaN;
+ }
return (
this._milliseconds +
this._days * 864e5 +
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;
};
}
// 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);
(m ? m + 'M' : '') +
(s ? s + 'S' : '');
}
+
+export function toJSON() {
+ // this may not be fully implemented
+ return this.isValid() ? this.toISOString() : null;
+}
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;
proto.humanize = humanize;
proto.toISOString = toISOString;
proto.toString = toISOString;
-proto.toJSON = toISOString;
+proto.toJSON = toJSON;
proto.locale = locale;
proto.localeData = localeData;
});
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');
});
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()));
// 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'})