Skip to content

Commit

Permalink
fix!: maybeCoerceNumber now takes precedence over coerce return value (
Browse files Browse the repository at this point in the history
  • Loading branch information
juergba authored and bcoe committed Jun 22, 2019
1 parent 7e01a2c commit 2f26436
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 2 additions & 2 deletions index.js
Expand Up @@ -497,7 +497,7 @@ function parse (args, opts) {
}

function maybeCoerceNumber (key, value) {
if (!checkAllAliases(key, flags.strings) && !checkAllAliases(key, flags.coercions)) {
if (!checkAllAliases(key, flags.strings)) {
const shouldCoerceNumber = isNumber(value) && configuration['parse-numbers'] && (
Number.isSafeInteger(Math.floor(value))
)
Expand Down Expand Up @@ -604,7 +604,7 @@ function parse (args, opts) {
coerce = checkAllAliases(key, flags.coercions)
if (typeof coerce === 'function') {
try {
var value = coerce(argv[key])
var value = maybeCoerceNumber(key, coerce(argv[key]))
;([].concat(flags.aliases[key] || [], key)).forEach(ali => {
applied[ali] = argv[ali] = value
})
Expand Down
8 changes: 6 additions & 2 deletions test/yargs-parser.js
Expand Up @@ -2147,14 +2147,18 @@ describe('yargs-parser', function () {
})

it('parses number if option explicitly set to number type', function () {
var parsed = parser(['--foo', '5', '--bar', '6'], {
number: 'bar',
var parsed = parser(['--foo', '5', '--bar', '6', '--baz', '7'], {
number: ['bar', 'baz'],
coerce: {
'baz': val => val
},
configuration: {
'parse-numbers': false
}
})
expect(parsed['foo']).to.equal('5')
expect(parsed['bar']).to.equal(6)
expect(parsed['baz']).to.equal(7)
})
})

Expand Down

0 comments on commit 2f26436

Please sign in to comment.