Skip to content

Commit 30005d3

Browse files
committed
maybeCoerceNumber(): don't loose array type,
adapt existing tests, add new tests
1 parent 47ccb0b commit 30005d3

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,9 @@ function parse (args, opts) {
501501
const shouldCoerceNumber = isNumber(value) && configuration['parse-numbers'] && (
502502
Number.isSafeInteger(Math.floor(value))
503503
)
504-
if (shouldCoerceNumber || (!isUndefined(value) && checkAllAliases(key, flags.numbers))) value = Number(value)
504+
if (shouldCoerceNumber || (!isUndefined(value) && checkAllAliases(key, flags.numbers))) {
505+
value = Array.isArray(value) ? [Number(value)] : Number(value)
506+
}
505507
}
506508
return value
507509
}

test/yargs-parser.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,7 +2191,7 @@ describe('yargs-parser', function () {
21912191

21922192
parsed['x'].should.deep.equal(['a', 'b'])
21932193
})
2194-
it('keeps only last argument', function () {
2194+
it('string: keeps only last argument', function () {
21952195
var parsed = parser('-x a -x b', {
21962196
configuration: {
21972197
'duplicate-arguments-array': false
@@ -2200,6 +2200,26 @@ describe('yargs-parser', function () {
22002200

22012201
parsed['x'].should.equal('b')
22022202
})
2203+
it('array[string]: keeps only last argument', function () {
2204+
var parsed = parser('-x a -x b', {
2205+
configuration: {
2206+
array: [{ key: 'x', string: true }],
2207+
'duplicate-arguments-array': false
2208+
}
2209+
})
2210+
2211+
parsed['x'].should.equal('b')
2212+
})
2213+
it('array[number]: keeps only last argument', function () {
2214+
var parsed = parser('-x 1 -x 2', {
2215+
configuration: {
2216+
array: [{ key: 'x', number: true }],
2217+
'duplicate-arguments-array': false
2218+
}
2219+
})
2220+
2221+
parsed['x'].should.equal(2)
2222+
})
22032223
it('does not interfere with nargs', function () {
22042224
var parsed = parser('-x a b c -x o p q', {
22052225
narg: { x: 3 },
@@ -2405,25 +2425,25 @@ describe('yargs-parser', function () {
24052425
})
24062426
describe('duplicate=true, flatten=false,', function () {
24072427
describe('type=array', function () {
2408-
it('[-x 1 -x 2 -x 3] => [1, 2, 3]', function () {
2428+
it('number: [-x 1 -x 2 -x 3] => [[1], [2], [3]]', function () {
24092429
var parsed = parser('-x 1 -x 2 -x 3', {
2410-
array: ['x'],
2430+
array: [{ key: 'x', number: true }],
24112431
configuration: {
24122432
'duplicate-arguments-array': true,
24132433
'flatten-duplicate-arrays': false
24142434
}
24152435
})
2416-
parsed['x'].should.deep.equal([1, 2, 3])
2436+
parsed['x'].should.deep.equal([[1], [2], [3]])
24172437
})
2418-
it('[-x 1 2 3 -x 2 3 4] => [[1, 2, 3], [ 2, 3, 4]]', function () {
2419-
var parsed = parser('-x 1 2 3 -x 2 3 4', {
2420-
array: ['x'],
2438+
it("string: [-x 1 -x 2 -x 3] => [['1'], ['2'], ['3']]", function () {
2439+
var parsed = parser('-x 1 -x 2 -x 3', {
2440+
array: [{ key: 'x', string: true }],
24212441
configuration: {
24222442
'duplicate-arguments-array': true,
24232443
'flatten-duplicate-arrays': false
24242444
}
24252445
})
2426-
parsed['x'].should.deep.equal([[1, 2, 3], [2, 3, 4]])
2446+
parsed['x'].should.deep.equal([['1'], ['2'], ['3']])
24272447
})
24282448
})
24292449
describe('type=number', function () {

0 commit comments

Comments
 (0)