Skip to content

Commit

Permalink
fix maybeCoerceNumber()
Browse files Browse the repository at this point in the history
  • Loading branch information
juergba committed Jun 10, 2019
1 parent 61faf08 commit 327b021
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
6 changes: 3 additions & 3 deletions index.js
Expand Up @@ -497,13 +497,13 @@ function parse (args, opts) {
}

function maybeCoerceNumber (key, value) {
if (Array.isArray(value)) return value

if (!checkAllAliases(key, flags.strings) && !checkAllAliases(key, flags.coercions)) {
const shouldCoerceNumber = isNumber(value) && configuration['parse-numbers'] && (
Number.isSafeInteger(Math.floor(value))
)
if (shouldCoerceNumber || (!isUndefined(value) && checkAllAliases(key, flags.numbers))) {
value = Array.isArray(value) ? [Number(value)] : Number(value)
}
if (shouldCoerceNumber || (!isUndefined(value) && checkAllAliases(key, flags.numbers))) value = Number(value)
}
return value
}
Expand Down
12 changes: 8 additions & 4 deletions test/yargs-parser.js
Expand Up @@ -2201,10 +2201,11 @@ describe('yargs-parser', function () {
parsed['x'].should.equal('b')
})
it('array[string]: keeps all arguments - ignores configuration', function () {
var parsed = parser('-x a -x b -y c', {
var parsed = parser('-x a -x b -y c -z d e', {
array: [
{ key: 'x', string: true },
{ key: 'y', string: true }
{ key: 'y', string: true },
{ key: 'z', string: true }
],
configuration: {
'duplicate-arguments-array': false
Expand All @@ -2213,12 +2214,14 @@ describe('yargs-parser', function () {

parsed['x'].should.deep.equal(['a', 'b'])
parsed['y'].should.deep.equal(['c'])
parsed['z'].should.deep.equal(['d', 'e'])
})
it('array[number]: keeps all arguments - ignores configuration', function () {
var parsed = parser('-x 1 -x 2 -y 3', {
var parsed = parser('-x 1 -x 2 -y 3 -z 4 5', {
array: [
{ key: 'x', number: true },
{ key: 'y', number: true }
{ key: 'y', number: true },
{ key: 'z', number: true }
],
configuration: {
'duplicate-arguments-array': false
Expand All @@ -2227,6 +2230,7 @@ describe('yargs-parser', function () {

parsed['x'].should.deep.equal([1, 2])
parsed['y'].should.deep.equal([3])
parsed['z'].should.deep.equal([4, 5])
})
it('does not interfere with nargs', function () {
var parsed = parser('-x a b c -x o p q', {
Expand Down

0 comments on commit 327b021

Please sign in to comment.