Skip to content

Commit c50873a

Browse files
committed
fix maybeCoerceNumber()
1 parent 61faf08 commit c50873a

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -497,13 +497,13 @@ function parse (args, opts) {
497497
}
498498

499499
function maybeCoerceNumber (key, value) {
500+
if (Array.isArray(value)) return value
501+
500502
if (!checkAllAliases(key, flags.strings) && !checkAllAliases(key, flags.coercions)) {
501503
const shouldCoerceNumber = isNumber(value) && configuration['parse-numbers'] && (
502504
Number.isSafeInteger(Math.floor(value))
503505
)
504-
if (shouldCoerceNumber || (!isUndefined(value) && checkAllAliases(key, flags.numbers))) {
505-
value = Array.isArray(value) ? [Number(value)] : Number(value)
506-
}
506+
if (shouldCoerceNumber || (!isUndefined(value) && checkAllAliases(key, flags.numbers))) value = Number(value)
507507
}
508508
return value
509509
}

test/yargs-parser.js

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2200,11 +2200,12 @@ describe('yargs-parser', function () {
22002200

22012201
parsed['x'].should.equal('b')
22022202
})
2203-
it('array[string]: keeps all arguments - ignores configuration', function () {
2204-
var parsed = parser('-x a -x b -y c', {
2203+
it('string[]: keeps all arguments - ignores configuration', function () {
2204+
var parsed = parser('-x a -x b -y c -z d e -z f g', {
22052205
array: [
22062206
{ key: 'x', string: true },
2207-
{ key: 'y', string: true }
2207+
{ key: 'y', string: true },
2208+
{ key: 'z', string: true }
22082209
],
22092210
configuration: {
22102211
'duplicate-arguments-array': false
@@ -2213,12 +2214,14 @@ describe('yargs-parser', function () {
22132214

22142215
parsed['x'].should.deep.equal(['a', 'b'])
22152216
parsed['y'].should.deep.equal(['c'])
2217+
parsed['z'].should.deep.equal(['d', 'e', 'f', 'g'])
22162218
})
2217-
it('array[number]: keeps all arguments - ignores configuration', function () {
2218-
var parsed = parser('-x 1 -x 2 -y 3', {
2219+
it('number[]]: keeps all arguments - ignores configuration', function () {
2220+
var parsed = parser('-x 1 -x 2 -y 3 -z 4 5 -z 6 7', {
22192221
array: [
22202222
{ key: 'x', number: true },
2221-
{ key: 'y', number: true }
2223+
{ key: 'y', number: true },
2224+
{ key: 'z', number: true }
22222225
],
22232226
configuration: {
22242227
'duplicate-arguments-array': false
@@ -2227,6 +2230,7 @@ describe('yargs-parser', function () {
22272230

22282231
parsed['x'].should.deep.equal([1, 2])
22292232
parsed['y'].should.deep.equal([3])
2233+
parsed['z'].should.deep.equal([4, 5, 6, 7])
22302234
})
22312235
it('does not interfere with nargs', function () {
22322236
var parsed = parser('-x a b c -x o p q', {
@@ -2298,14 +2302,14 @@ describe('yargs-parser', function () {
22982302
duplicate=false, flatten=false
22992303
type=array
23002304
[-x 1 2 3] => [1, 2, 3]
2301-
[-x 1 2 3 -x 2 3 4] => [2, 3, 4]
2305+
[-x 1 2 3 -x 2 3 4] => [[1, 2, 3], [2, 3, 4]]
23022306
type=string/number/etc
23032307
[-x 1 -x 2 -x 3] => 3
23042308
23052309
duplicate=false, flatten=true
23062310
type=array
23072311
[-x 1 2 3] => [1, 2, 3]
2308-
[-x 1 2 3 -x 2 3 4] => [2, 3, 4]
2312+
[-x 1 2 3 -x 2 3 4] => [1, 2, 3, 2, 3, 4]
23092313
type=string/number/etc
23102314
[-x 1 -x 2 -x 3] => 3
23112315
@@ -2435,25 +2439,33 @@ describe('yargs-parser', function () {
24352439
})
24362440
describe('duplicate=true, flatten=false,', function () {
24372441
describe('type=array', function () {
2438-
it('number: [-x 1 -x 2 -x 3] => [[1], [2], [3]]', function () {
2439-
var parsed = parser('-x 1 -x 2 -x 3', {
2440-
array: [{ key: 'x', number: true }],
2442+
it('number[]: [-x 1 -x 2 -x 3] => [[1], [2], [3]]', function () {
2443+
var parsed = parser('-x 1 -x 2 -x 3 -y 4 5 -y 6 7', {
2444+
array: [
2445+
{ key: 'x', number: true },
2446+
{ key: 'y', number: true }
2447+
],
24412448
configuration: {
24422449
'duplicate-arguments-array': true,
24432450
'flatten-duplicate-arrays': false
24442451
}
24452452
})
24462453
parsed['x'].should.deep.equal([[1], [2], [3]])
2454+
parsed['y'].should.deep.equal([[4, 5], [6, 7]])
24472455
})
2448-
it("string: [-x 1 -x 2 -x 3] => [['1'], ['2'], ['3']]", function () {
2449-
var parsed = parser('-x 1 -x 2 -x 3', {
2450-
array: [{ key: 'x', string: true }],
2456+
it("string[]: [-x 1 -x 2 -x 3] => [['1'], ['2'], ['3']]", function () {
2457+
var parsed = parser('-x 1 -x 2 -x 3 -y 4 5 -y 6 7', {
2458+
array: [
2459+
{ key: 'x', string: true },
2460+
{ key: 'y', string: true }
2461+
],
24512462
configuration: {
24522463
'duplicate-arguments-array': true,
24532464
'flatten-duplicate-arrays': false
24542465
}
24552466
})
24562467
parsed['x'].should.deep.equal([['1'], ['2'], ['3']])
2468+
parsed['y'].should.deep.equal([['4', '5'], ['6', '7']])
24572469
})
24582470
})
24592471
describe('type=number', function () {

0 commit comments

Comments
 (0)