From 3d8f3b557c115301fce861a4ea85e96b771f1db1 Mon Sep 17 00:00:00 2001 From: juergba Date: Fri, 22 Mar 2019 11:15:25 +0100 Subject: [PATCH] maybeCoerceNumber(): don't loose array type, adapt existing tests, add new tests --- index.js | 4 +++- test/yargs-parser.js | 36 ++++++++++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 20d472d1..f7fdd71a 100644 --- a/index.js +++ b/index.js @@ -484,7 +484,9 @@ function parse (args, opts) { const shouldCoerceNumber = isNumber(value) && configuration['parse-numbers'] && ( Number.isSafeInteger(Math.floor(value)) ) - if (shouldCoerceNumber || (!isUndefined(value) && checkAllAliases(key, flags.numbers))) value = Number(value) + if (shouldCoerceNumber || (!isUndefined(value) && checkAllAliases(key, flags.numbers))) { + value = Array.isArray(value) ? [Number(value)] : Number(value) + } } return value } diff --git a/test/yargs-parser.js b/test/yargs-parser.js index ee76e417..ba049756 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -2180,7 +2180,7 @@ describe('yargs-parser', function () { parsed['x'].should.deep.equal(['a', 'b']) }) - it('keeps only last argument', function () { + it('string: keeps only last argument', function () { var parsed = parser('-x a -x b', { configuration: { 'duplicate-arguments-array': false @@ -2189,6 +2189,26 @@ describe('yargs-parser', function () { parsed['x'].should.equal('b') }) + it('array[string]: keeps only last argument', function () { + var parsed = parser('-x a -x b', { + configuration: { + array: [{ key: 'x', string: true }], + 'duplicate-arguments-array': false + } + }) + + parsed['x'].should.equal('b') + }) + it('array[number]: keeps only last argument', function () { + var parsed = parser('-x 1 -x 2', { + configuration: { + array: [{ key: 'x', number: true }], + 'duplicate-arguments-array': false + } + }) + + parsed['x'].should.equal(2) + }) }) describe('flatten duplicate arrays', function () { @@ -2385,25 +2405,25 @@ 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('number: [-x 1 -x 2 -x 3] => [[1], [2], [3]', function () { var parsed = parser('-x 1 -x 2 -x 3', { - array: ['x'], + array: [{ key: 'x', number: true }], configuration: { 'duplicate-arguments-array': true, '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', { - array: ['x'], + it('string: [-x 1 -x 2 -x 3] => [[1], [2], [3]', function () { + var parsed = parser('-x 1 -x 2 -x 3', { + array: [{ key: 'x', string: true }], configuration: { 'duplicate-arguments-array': true, 'flatten-duplicate-arrays': false } }) - parsed['x'].should.deep.equal([[1, 2, 3], [2, 3, 4]]) + parsed['x'].should.deep.equal([['1'], ['2'], ['3']]) }) }) describe('type=number', function () {