From: Zach Gawlik Date: Sat, 3 Feb 2018 22:31:19 +0000 (-0500) Subject: [feature] Add support for creating duration from numeric string X-Git-Tag: 2.25.0~33^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a021ade5d74c282de0743a78deec7b866c185e36;p=thirdparty%2Fmoment.git [feature] Add support for creating duration from numeric string 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 --- diff --git a/src/lib/duration/create.js b/src/lib/duration/create.js index 568f1c19c..b5d03cdbe 100644 --- a/src/lib/duration/create.js +++ b/src/lib/duration/create.js @@ -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; diff --git a/src/lib/moment/add-subtract.js b/src/lib/moment/add-subtract.js index e758b5d46..4818ed6b8 100644 --- a/src/lib/moment/add-subtract.js +++ b/src/lib/moment/add-subtract.js @@ -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; diff --git a/src/test/moment/duration.js b/src/test/moment/duration.js index e0f5212ea..53fe125da 100644 --- a/src/test/moment/duration.js +++ b/src/test/moment/duration.js @@ -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');