Skip to content

Commit 4ca69da

Browse files
maxrimuebcoe
authored andcommitted
fix: coerce should be applied to the final objects and arrays created (#57)
BREAKING CHANGE: coerce is no longer applied to individual arguments in an implicit array.
1 parent 2f02003 commit 4ca69da

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

index.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ function parse (args, opts) {
280280
setConfig(argv)
281281
setConfigObjects()
282282
applyEnvVars(argv, false)
283-
applyArrayCoercions(argv)
283+
applyCoercions(argv)
284284
applyDefaultsAndAliases(argv, flags.aliases, defaults)
285285

286286
// for any counts either not in args or without an explicit default, set to 0
@@ -483,11 +483,9 @@ function parse (args, opts) {
483483
})
484484
}
485485

486-
function applyArrayCoercions (argv) {
486+
function applyCoercions (argv) {
487487
var coerce
488-
Object.keys(argv).filter(function (key) {
489-
return key === '_' || checkAllAliases(key, flags.arrays)
490-
}).forEach(function (key) {
488+
Object.keys(argv).forEach(function (key) {
491489
coerce = checkAllAliases(key, flags.coercions)
492490
if (typeof coerce === 'function') {
493491
try {
@@ -538,14 +536,6 @@ function parse (args, opts) {
538536
})
539537

540538
var key = keys[keys.length - 1]
541-
var coerce = !checkAllAliases(key, flags.arrays) && checkAllAliases(key, flags.coercions)
542-
if (typeof coerce === 'function') {
543-
try {
544-
value = coerce(value)
545-
} catch (err) {
546-
error = err
547-
}
548-
}
549539

550540
if (value === increment) {
551541
o[key] = increment(o[key])

test/yargs-parser.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1980,11 +1980,25 @@ describe('yargs-parser', function () {
19801980
parsed.f.should.equal(-99)
19811981
})
19821982

1983+
it('applies coercion function to all dot options', function () {
1984+
var parsed = parser(['--foo.bar', 'nananana'], {
1985+
coerce: {
1986+
foo: function (val) {
1987+
val.bar += ', batman!'
1988+
return val
1989+
}
1990+
}
1991+
})
1992+
parsed.foo.bar.should.equal('nananana, batman!')
1993+
})
1994+
19831995
it('applies coercion function to an implicit array', function () {
19841996
var parsed = parser(['--foo', '99', '-f', '33'], {
19851997
coerce: {
19861998
f: function (arg) {
1987-
return arg * -1
1999+
return arg.map(function (a) {
2000+
return a * -1
2001+
})
19882002
}
19892003
},
19902004
alias: {

0 commit comments

Comments
 (0)