]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
fix durations not being cloned properly 863/head
authorFraser Tweedale <frase@frase.id.au>
Thu, 20 Jun 2013 08:58:35 +0000 (18:58 +1000)
committerFraser Tweedale <frase@frase.id.au>
Mon, 24 Jun 2013 06:29:27 +0000 (16:29 +1000)
``moment.duration`` does not return an equivalent Duration for all
inputs.  Add a test for this and fix the issue by reusing the
Duration's constructor argument.

moment.js
test/moment/duration.js

index 74b93e165ad44e836dfff11a15d2dfc051da4600..65de1d7b0a2ce00375e9c48dbcb62fe2332e47a8 100644 (file)
--- a/moment.js
+++ b/moment.js
             seconds = duration.seconds || duration.second || duration.s || 0,
             milliseconds = duration.milliseconds || duration.millisecond || duration.ms || 0;
 
+        // store reference to input for deterministic cloning
+        this._input = duration;
+
         // representation for dateAddRemove
         this._milliseconds = milliseconds +
             seconds * 1e3 + // 1000
     moment.duration = function (input, key) {
         var isDuration = moment.isDuration(input),
             isNumber = (typeof input === 'number'),
-            duration = (isDuration ? input._data : (isNumber ? {} : input)),
+            duration = (isDuration ? input._input : (isNumber ? {} : input)),
             matched = aspNetTimeSpanJsonRegex.exec(input),
             sign,
             ret;
index eee2b59e1f346bce75880f1d73ae6446ce7a7b91..66ba623583f908ffd4f89638129547667460cd9b 100644 (file)
@@ -96,6 +96,7 @@ exports.duration = {
 
     "instantiation from another duration" : function(test) {
         var simple = moment.duration(1234),
+            lengthy = moment.duration(60 * 60 * 24 * 360 * 1e3),
             complicated = moment.duration({
                 years: 2,
                 months: 3,
@@ -107,8 +108,9 @@ exports.duration = {
                 milliseconds: 12
             });
 
-        test.expect(2);
+        test.expect(3);
         test.deepEqual(moment.duration(simple), simple, "simple clones are equal");
+        test.deepEqual(moment.duration(lengthy), lengthy, "lengthy clones are equal");
         test.deepEqual(moment.duration(complicated), complicated, "complicated clones are equal");
         test.done();
     },