]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
[feature] Add support for creating duration from numeric string
authorZach Gawlik <ZachGawlik@users.noreply.github.com>
Sat, 3 Feb 2018 22:31:19 +0000 (17:31 -0500)
committerIskren Chernev <iskren.chernev@gmail.com>
Sat, 25 Apr 2020 22:39:34 +0000 (01:39 +0300)
Similar to other moment methods, a string representing a number is
(unfortunately) supported. This change adds support in duration
constructor.

    moment.duration('5')            # 5 milliseconds
    moment.duration('5', 'seconds') # 5 seconds

src/lib/duration/create.js
src/lib/moment/add-subtract.js
src/test/moment/duration.js

index 568f1c19c496c5cb69a53f7acb071f92ba84999d..b5d03cdbeca4d77b3feed645dd220ce60d3d649f 100644 (file)
@@ -30,12 +30,12 @@ export function createDuration (input, key) {
             d  : input._days,
             M  : input._months
         };
-    } else if (isNumber(input)) {
+    } else if (isNumber(input) || !isNaN(+input)) {
         duration = {};
         if (key) {
-            duration[key] = input;
+            duration[key] = +input;
         } else {
-            duration.milliseconds = input;
+            duration.milliseconds = +input;
         }
     } else if (!!(match = aspNetRegex.exec(input))) {
         sign = (match[1] === '-') ? -1 : 1;
index e758b5d46609b18ead3d18e52aeec2a75c3a5ba8..4818ed6b8275323dcc75626a10b713cc7232bbad 100644 (file)
@@ -17,7 +17,6 @@ function createAdder(direction, name) {
             tmp = val; val = period; period = tmp;
         }
 
-        val = typeof val === 'string' ? +val : val;
         dur = createDuration(val, period);
         addSubtract(this, dur, direction);
         return this;
index e0f5212ea7572062516effff999035de506610b5..53fe125da7c62585e78f9a5622257047b926308f 100644 (file)
@@ -52,6 +52,11 @@ test('milliseconds instantiation', function (assert) {
     assert.equal(moment.duration(72).humanize(), 'a few seconds', 'Duration should be valid');
 });
 
+test('milliseconds instantiation with string', function (assert) {
+    assert.equal(moment.duration('72').milliseconds(), 72, 'milliseconds');
+    assert.equal(moment.duration('72').humanize(), 'a few seconds', 'Duration should be valid');
+});
+
 test('undefined instantiation', function (assert) {
     assert.equal(moment.duration(undefined).milliseconds(), 0, 'milliseconds');
     assert.equal(moment.duration(undefined).isValid(), true, '_isValid');
@@ -89,6 +94,25 @@ test('instantiation by type', function (assert) {
     assert.equal(moment.duration(8, 'ms').milliseconds(),             8, 'ms');
 });
 
+test('instantiation by type with string', function (assert) {
+    assert.equal(moment.duration('1', 'years').years(),                 1, 'years');
+    assert.equal(moment.duration('1', 'y').years(),                     1, 'y');
+    assert.equal(moment.duration('2', 'months').months(),               2, 'months');
+    assert.equal(moment.duration('2', 'M').months(),                    2, 'M');
+    assert.equal(moment.duration('3', 'weeks').weeks(),                 3, 'weeks');
+    assert.equal(moment.duration('3', 'w').weeks(),                     3, 'weeks');
+    assert.equal(moment.duration('4', 'days').days(),                   4, 'days');
+    assert.equal(moment.duration('4', 'd').days(),                      4, 'd');
+    assert.equal(moment.duration('5', 'hours').hours(),                 5, 'hours');
+    assert.equal(moment.duration('5', 'h').hours(),                     5, 'h');
+    assert.equal(moment.duration('6', 'minutes').minutes(),             6, 'minutes');
+    assert.equal(moment.duration('6', 'm').minutes(),                   6, 'm');
+    assert.equal(moment.duration('7', 'seconds').seconds(),             7, 'seconds');
+    assert.equal(moment.duration('7', 's').seconds(),                   7, 's');
+    assert.equal(moment.duration('8', 'milliseconds').milliseconds(),   8, 'milliseconds');
+    assert.equal(moment.duration('8', 'ms').milliseconds(),             8, 'ms');
+});
+
 test('shortcuts', function (assert) {
     assert.equal(moment.duration({y: 1}).years(),         1, 'years = y');
     assert.equal(moment.duration({M: 2}).months(),        2, 'months = M');