From 2f2643602f6310078c6ec141bd395435818f90ef Mon Sep 17 00:00:00 2001 From: Juerg B <44573692+juergba@users.noreply.github.com> Date: Sun, 23 Jun 2019 00:35:03 +0200 Subject: [PATCH] fix!: maybeCoerceNumber now takes precedence over coerce return value (#182) --- index.js | 4 ++-- test/yargs-parser.js | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index a6c95ba3..315d1aab 100644 --- a/index.js +++ b/index.js @@ -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)) ) @@ -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 }) diff --git a/test/yargs-parser.js b/test/yargs-parser.js index a8628261..c908187b 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -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) }) })