From d909d28e50d7012895cef2c75c0a50c56cbb8401 Mon Sep 17 00:00:00 2001 From: Rocky Meza Date: Sun, 15 Apr 2012 00:23:27 -0600 Subject: [PATCH] ensure that you can create durations from an actual duration object --- moment.js | 10 ++++++++-- test/moment/duration.js | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/moment.js b/moment.js index 476b29845..ea468625f 100644 --- a/moment.js +++ b/moment.js @@ -475,8 +475,9 @@ // duration moment.duration = function (input, key) { - var isNumber = (typeof input === 'number'), - duration = isNumber ? {} : input; + var isDuration = moment.isDuration(input), + isNumber = (typeof input === 'number'), + duration = (isDuration ? input._data : (isNumber ? {} : input)); if (isNumber) { if (key) { @@ -579,6 +580,11 @@ return obj instanceof Moment; }; + // for typechecking Duration objects + moment.isDuration = function (obj) { + return obj instanceof Duration; + }; + // shortcut for prototype moment.fn = Moment.prototype = { diff --git a/test/moment/duration.js b/test/moment/duration.js index 6cab93a37..cd5aa40e5 100644 --- a/test/moment/duration.js +++ b/test/moment/duration.js @@ -65,6 +65,25 @@ exports.duration = { test.done(); }, + "instantiation from another duration" : function(test) { + var simple = moment.duration(1234), + complicated = moment.duration({ + years: 2, + months: 3, + weeks: 4, + days: 1, + hours: 8, + minutes: 9, + seconds: 20, + milliseconds: 12 + }); + + test.expect(2); + test.deepEqual(moment.duration(simple), simple, "simple clones are equal"); + test.deepEqual(moment.duration(complicated), complicated, "complicated clones are equal"); + test.done(); + }, + "humanize" : function(test) { test.expect(32); moment.lang('en'); @@ -109,5 +128,13 @@ exports.duration = { test.equal(moment.duration({seconds: 44}).humanize(true), "in a few seconds", "44 seconds = a few seconds"); test.equal(moment.duration({seconds: -44}).humanize(true), "a few seconds ago", "44 seconds = a few seconds"); test.done(); + }, + + "isDuration" : function(test) { + test.expect(3); + test.ok(moment.isDuration(moment.duration(12345678)), "correctly says true"); + test.ok(!moment.isDuration(moment()), "moment object is not a duration"); + test.ok(!moment.isDuration({milliseconds: 1}), "plain object is not a duration"); + test.done(); } }; -- 2.47.3