]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Fix Duration#isValid after #add or #subtract
authorLucas Sanders <butterflyhug@google.com>
Sun, 19 Mar 2017 02:49:28 +0000 (22:49 -0400)
committerLucas Sanders <butterflyhug@google.com>
Wed, 22 Mar 2017 11:00:19 +0000 (07:00 -0400)
Also make the Duration#add and Duration#subtract implementations
internally immutable (so we don't need to wrap them for the
prototype).

src/lib/duration/add-subtract.js
src/lib/duration/prototype.js
src/test/moment/duration_invalid.js

index 3b44e186fcc21a21444698fa41690b172a6be18c..bfc1a372365753a35c75d56e7173c2cd77fe00d0 100644 (file)
@@ -1,13 +1,17 @@
+import { isDuration } from './constructor';
 import { createDuration } from './create';
+import { createInvalid } from './valid';
 
 function addSubtract (duration, input, value, direction) {
-    var other = createDuration(input, value);
-
-    duration._milliseconds += direction * other._milliseconds;
-    duration._days         += direction * other._days;
-    duration._months       += direction * other._months;
-
-    return duration._bubble();
+    var other = isDuration(input) ? input : createDuration(input, value);
+    if (!duration.isValid() || !other.isValid()) {
+        return createInvalid();
+    }
+    return createDuration({
+        ms: duration._milliseconds + direction * other._milliseconds,
+        d:  duration._days         + direction * other._days,
+        M:  duration._months       + direction * other._months
+    });
 }
 
 // supports only 2.0-style add(1, 's') or add(duration)
index 7eaca44612c7656a2402983eaa0bc46f25942151..ff6c70ae7e7b6ab667e25e60b4045eb3ad335caf 100644 (file)
@@ -15,8 +15,8 @@ import { isValid } from './valid';
 
 proto.isValid        = isValid;
 proto.abs            = abs;
-proto.add            = wrap(Duration, add);
-proto.subtract       = wrap(Duration, subtract);
+proto.add            = add;
+proto.subtract       = subtract;
 proto.as             = as;
 proto.asMilliseconds = asMilliseconds;
 proto.asSeconds      = asSeconds;
index 4b023a880c5fff7a816d7b472f84f8fa54b3104c..acfe3c02a0fa24659cbde35055e8f3e4b5980160 100644 (file)
@@ -1,7 +1,7 @@
 import { module, test } from '../qunit';
 import moment from '../../moment';
 
-module('invalid');
+module('duration invalid');
 
 test('invalid duration', function (assert) {
     var m = moment.duration.invalid(); // should be invalid