Skip to content

Commit

Permalink
maybeCoerceNumber(): don't loose array type,
Browse files Browse the repository at this point in the history
adapt existing tests,
add new tests
  • Loading branch information
juergba committed Jun 10, 2019
1 parent 7e01a2c commit 8262187
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
36 changes: 28 additions & 8 deletions test/yargs-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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', {
array: [{ key: 'x', string: true }],
configuration: {
'duplicate-arguments-array': false
}
})

parsed['x'].should.equal('b')
})
it('array[number]: keeps only last argument', function () {
var parsed = parser('-x 1 -x 2', {
array: [{ key: 'x', number: true }],
configuration: {
'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 },
Expand Down Expand Up @@ -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 () {
Expand Down

0 comments on commit 8262187

Please sign in to comment.