From 30005d389820eaed22c85ab76422bf5b4c7e1630 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 a6c95ba3..aaf69f25 100644 --- a/index.js +++ b/index.js @@ -501,7 +501,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 a8628261..15bb4b86 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -2191,7 +2191,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 @@ -2200,6 +2200,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) + }) it('does not interfere with nargs', function () { var parsed = parser('-x a b c -x o p q', { narg: { x: 3 }, @@ -2405,25 +2425,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 () {