From 52b0e87e76c7acefada52b7b8af15eaae5483fbe Mon Sep 17 00:00:00 2001 From: juergba Date: Fri, 12 Jul 2019 09:01:02 +0200 Subject: [PATCH 1/3] process values of number[] --- index.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 4fd05531..11eac1e5 100644 --- a/index.js +++ b/index.js @@ -470,7 +470,9 @@ function parse (args, opts) { if (typeof val === 'string') val = val === 'true' } - var value = maybeCoerceNumber(key, val) + var value = Array.isArray(val) + ? val.map(function (v) { return maybeCoerceNumber(key, v) }) + : maybeCoerceNumber(key, val) // increment a count given as arg (either no value or value parsed as boolean) if (checkAllAliases(key, flags.counts) && (isUndefined(value) || typeof value === 'boolean')) { @@ -486,7 +488,7 @@ function parse (args, opts) { } function maybeCoerceNumber (key, value) { - if (!checkAllAliases(key, flags.strings)) { + if (!checkAllAliases(key, flags.strings) && !checkAllAliases(key, flags.bools) && !Array.isArray(value)) { const shouldCoerceNumber = isNumber(value) && configuration['parse-numbers'] && ( Number.isSafeInteger(Math.floor(value)) ) From 95ff7d3aebc1a98b85d69cdde9c5bb0b99dd54d7 Mon Sep 17 00:00:00 2001 From: juergba Date: Fri, 12 Jul 2019 09:01:18 +0200 Subject: [PATCH 2/3] adapt existing test --- test/yargs-parser.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/yargs-parser.js b/test/yargs-parser.js index abcfaafd..c1996b1c 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -2445,7 +2445,7 @@ describe('yargs-parser', function () { }) describe('duplicate=true, flatten=false,', function () { describe('type=array', function () { - it('[-x 1 -x 2 -x 3] => [1, 2, 3]', function () { + it('[-x 1 -x 2 -x 3] => [[1], [2], [3]]', function () { var parsed = parser('-x 1 -x 2 -x 3', { array: ['x'], configuration: { @@ -2453,7 +2453,7 @@ describe('yargs-parser', function () { 'flatten-duplicate-arrays': false } }) - parsed['x'].should.deep.equal([1, 2, 3]) + parsed['x'].should.deep.equal([[1], [2], [3]]) }) it('[-x 1 2 3 -x 2 3 4] => [[1, 2, 3], [ 2, 3, 4]]', function () { var parsed = parser('-x 1 2 3 -x 2 3 4', { From b5be3a842c5d817873b2eae955ef9b40888b4c56 Mon Sep 17 00:00:00 2001 From: juergba Date: Fri, 12 Jul 2019 10:05:18 +0200 Subject: [PATCH 3/3] additional test --- test/yargs-parser.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/yargs-parser.js b/test/yargs-parser.js index c1996b1c..773cfe32 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -2160,6 +2160,19 @@ describe('yargs-parser', function () { expect(parsed['bar']).to.equal(6) expect(parsed['baz']).to.equal(7) }) + + it('should coerce elements of number typed arrays to numbers', function () { + var parsed = parser(['--foo', '4', '--foo', '5', '2'], { + array: ['foo'], + configObjects: [{ foo: ['1', '2', '3'] }], + configuration: { + 'combine-arrays': true, + 'flatten-duplicate-arrays': false + } + }) + + expect(parsed['foo']).to.deep.equal([[4], [5, 2], [1, 2, 3]]) + }) }) describe('boolean negation', function () {