From: Chasen Le Hara Date: Fri, 19 Dec 2014 08:40:44 +0000 (-0800) Subject: Create valid duration object when null is passed to moment.duration() X-Git-Tag: 2.9.0~8^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=583f380740468db843154df31fa72647c6173d17;p=thirdparty%2Fmoment.git Create valid duration object when null is passed to moment.duration() --- diff --git a/moment.js b/moment.js index 19a416929..e4fbb6e87 100644 --- a/moment.js +++ b/moment.js @@ -1998,6 +1998,8 @@ s: parseIso(match[7]), w: parseIso(match[8]) }; + } else if (duration == null) {// checks for null or undefined + duration = {}; } else if (typeof duration === 'object' && ('from' in duration || 'to' in duration)) { diffRes = momentsDifference(moment(duration.from), moment(duration.to)); diff --git a/test/moment/duration.js b/test/moment/duration.js index a640bc41e..af1c0da84 100644 --- a/test/moment/duration.js +++ b/test/moment/duration.js @@ -62,6 +62,18 @@ exports.duration = { test.done(); }, + 'undefined instantiation' : function (test) { + test.expect(1); + test.equal(moment.duration(undefined).milliseconds(), 0, 'milliseconds'); + test.done(); + }, + + 'null instantiation' : function (test) { + test.expect(1); + test.equal(moment.duration(null).milliseconds(), 0, 'milliseconds'); + test.done(); + }, + 'instantiation by type' : function (test) { test.expect(16); test.equal(moment.duration(1, 'years').years(), 1, 'years');