Skip to content

Commit

Permalink
Merge pull request moment#2111 from chasenlehara:nullDuration
Browse files Browse the repository at this point in the history
Create valid duration object when null is passed to moment.duration()
  • Loading branch information
ichernev committed Dec 29, 2014
2 parents 5d6968c + 583f380 commit 75ed8e6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions moment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
12 changes: 12 additions & 0 deletions test/moment/duration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 75ed8e6

Please sign in to comment.