]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
ensure that you can create durations from an actual duration object
authorRocky Meza <rocky@fusionbox.com>
Sun, 15 Apr 2012 06:23:27 +0000 (00:23 -0600)
committerRocky Meza <rocky@fusionbox.com>
Sun, 15 Apr 2012 06:23:27 +0000 (00:23 -0600)
moment.js
test/moment/duration.js

index 476b29845def245b76da1bf4ee3a683353ff5848..ea468625f5f96d9ac8382269997ea01c6589afdc 100644 (file)
--- a/moment.js
+++ b/moment.js
 
     // 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) {
         return obj instanceof Moment;
     };
 
+    // for typechecking Duration objects
+    moment.isDuration = function (obj) {
+        return obj instanceof Duration;
+    };
+
     // shortcut for prototype
     moment.fn = Moment.prototype = {
 
index 6cab93a379c8e1afad17baa96f3f2c40bf262927..cd5aa40e55d420fc90bd33fee2913a13403ea3b5 100644 (file)
@@ -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();
     }
 };