Skip to content

Commit 3d8f3b5

Browse files
committed
maybeCoerceNumber(): don't loose array type,
adapt existing tests, add new tests
1 parent 1404f79 commit 3d8f3b5

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
@@ -484,7 +484,9 @@ function parse (args, opts) {
484484
const shouldCoerceNumber = isNumber(value) && configuration['parse-numbers'] && (
485485
Number.isSafeInteger(Math.floor(value))
486486
)
487-
if (shouldCoerceNumber || (!isUndefined(value) && checkAllAliases(key, flags.numbers))) value = Number(value)
487+
if (shouldCoerceNumber || (!isUndefined(value) && checkAllAliases(key, flags.numbers))) {
488+
value = Array.isArray(value) ? [Number(value)] : Number(value)
489+
}
488490
}
489491
return value
490492
}

test/yargs-parser.js

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

21812181
parsed['x'].should.deep.equal(['a', 'b'])
21822182
})
2183-
it('keeps only last argument', function () {
2183+
it('string: keeps only last argument', function () {
21842184
var parsed = parser('-x a -x b', {
21852185
configuration: {
21862186
'duplicate-arguments-array': false
@@ -2189,6 +2189,26 @@ describe('yargs-parser', function () {
21892189

21902190
parsed['x'].should.equal('b')
21912191
})
2192+
it('array[string]: keeps only last argument', function () {
2193+
var parsed = parser('-x a -x b', {
2194+
configuration: {
2195+
array: [{ key: 'x', string: true }],
2196+
'duplicate-arguments-array': false
2197+
}
2198+
})
2199+
2200+
parsed['x'].should.equal('b')
2201+
})
2202+
it('array[number]: keeps only last argument', function () {
2203+
var parsed = parser('-x 1 -x 2', {
2204+
configuration: {
2205+
array: [{ key: 'x', number: true }],
2206+
'duplicate-arguments-array': false
2207+
}
2208+
})
2209+
2210+
parsed['x'].should.equal(2)
2211+
})
21922212
})
21932213

21942214
describe('flatten duplicate arrays', function () {
@@ -2385,25 +2405,25 @@ describe('yargs-parser', function () {
23852405
})
23862406
describe('duplicate=true, flatten=false,', function () {
23872407
describe('type=array', function () {
2388-
it('[-x 1 -x 2 -x 3] => [1, 2, 3]', function () {
2408+
it('number: [-x 1 -x 2 -x 3] => [[1], [2], [3]', function () {
23892409
var parsed = parser('-x 1 -x 2 -x 3', {
2390-
array: ['x'],
2410+
array: [{ key: 'x', number: true }],
23912411
configuration: {
23922412
'duplicate-arguments-array': true,
23932413
'flatten-duplicate-arrays': false
23942414
}
23952415
})
2396-
parsed['x'].should.deep.equal([1, 2, 3])
2416+
parsed['x'].should.deep.equal([[1], [2], [3]])
23972417
})
2398-
it('[-x 1 2 3 -x 2 3 4] => [[1, 2, 3], [ 2, 3, 4]]', function () {
2399-
var parsed = parser('-x 1 2 3 -x 2 3 4', {
2400-
array: ['x'],
2418+
it('string: [-x 1 -x 2 -x 3] => [[1], [2], [3]', function () {
2419+
var parsed = parser('-x 1 -x 2 -x 3', {
2420+
array: [{ key: 'x', string: true }],
24012421
configuration: {
24022422
'duplicate-arguments-array': true,
24032423
'flatten-duplicate-arrays': false
24042424
}
24052425
})
2406-
parsed['x'].should.deep.equal([[1, 2, 3], [2, 3, 4]])
2426+
parsed['x'].should.deep.equal([['1'], ['2'], ['3']])
24072427
})
24082428
})
24092429
describe('type=number', function () {

0 commit comments

Comments
 (0)