From 327b0213761f6fd31b71b908cbe475983daa7e89 Mon Sep 17 00:00:00 2001 From: juergba Date: Mon, 10 Jun 2019 19:13:41 +0200 Subject: [PATCH] fix maybeCoerceNumber() --- index.js | 6 +++--- test/yargs-parser.js | 12 ++++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 83e1a0e3..c29f0c46 100644 --- a/index.js +++ b/index.js @@ -497,13 +497,13 @@ function parse (args, opts) { } function maybeCoerceNumber (key, value) { + if (Array.isArray(value)) return value + if (!checkAllAliases(key, flags.strings) && !checkAllAliases(key, flags.coercions)) { const shouldCoerceNumber = isNumber(value) && configuration['parse-numbers'] && ( Number.isSafeInteger(Math.floor(value)) ) - if (shouldCoerceNumber || (!isUndefined(value) && checkAllAliases(key, flags.numbers))) { - value = Array.isArray(value) ? [Number(value)] : Number(value) - } + if (shouldCoerceNumber || (!isUndefined(value) && checkAllAliases(key, flags.numbers))) value = Number(value) } return value } diff --git a/test/yargs-parser.js b/test/yargs-parser.js index 6d20eda3..9b0192d7 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -2201,10 +2201,11 @@ describe('yargs-parser', function () { parsed['x'].should.equal('b') }) it('array[string]: keeps all arguments - ignores configuration', function () { - var parsed = parser('-x a -x b -y c', { + var parsed = parser('-x a -x b -y c -z d e', { array: [ { key: 'x', string: true }, - { key: 'y', string: true } + { key: 'y', string: true }, + { key: 'z', string: true } ], configuration: { 'duplicate-arguments-array': false @@ -2213,12 +2214,14 @@ describe('yargs-parser', function () { parsed['x'].should.deep.equal(['a', 'b']) parsed['y'].should.deep.equal(['c']) + parsed['z'].should.deep.equal(['d', 'e']) }) it('array[number]: keeps all arguments - ignores configuration', function () { - var parsed = parser('-x 1 -x 2 -y 3', { + var parsed = parser('-x 1 -x 2 -y 3 -z 4 5', { array: [ { key: 'x', number: true }, - { key: 'y', number: true } + { key: 'y', number: true }, + { key: 'z', number: true } ], configuration: { 'duplicate-arguments-array': false @@ -2227,6 +2230,7 @@ describe('yargs-parser', function () { parsed['x'].should.deep.equal([1, 2]) parsed['y'].should.deep.equal([3]) + parsed['z'].should.deep.equal([4, 5]) }) it('does not interfere with nargs', function () { var parsed = parser('-x a b c -x o p q', {