Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: coerce full array instead of each element #51

Merged
merged 2 commits into from
Aug 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ function parse (args, opts) {
setConfig(argv)
setConfigObjects()
applyEnvVars(argv, false)
applyArrayCoercions(argv)
applyDefaultsAndAliases(argv, flags.aliases, defaults)

// for any counts either not in args or without an explicit default, set to 0
Expand Down Expand Up @@ -482,6 +483,22 @@ function parse (args, opts) {
})
}

function applyArrayCoercions (argv) {
var coerce
Object.keys(argv).filter(function (key) {
return key === '_' || checkAllAliases(key, flags.arrays)
}).forEach(function (key) {
coerce = checkAllAliases(key, flags.coercions)
if (typeof coerce === 'function') {
try {
argv[key] = coerce(argv[key])
} catch (err) {
error = err
}
}
})
}

function applyDefaultsAndAliases (obj, aliases, defaults) {
Object.keys(defaults).forEach(function (key) {
if (!hasKey(obj, key.split('.'))) {
Expand Down Expand Up @@ -521,7 +538,7 @@ function parse (args, opts) {
})

var key = keys[keys.length - 1]
var coerce = checkAllAliases(key, flags.coercions)
var coerce = !checkAllAliases(key, flags.arrays) && checkAllAliases(key, flags.coercions)
if (typeof coerce === 'function') {
try {
value = coerce(value)
Expand Down
46 changes: 44 additions & 2 deletions test/yargs-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1980,13 +1980,30 @@ describe('yargs-parser', function () {
parsed.f.should.equal(-99)
})

it('applies coercion function to an array', function () {
it('applies coercion function to an implicit array', function () {
var parsed = parser(['--foo', '99', '-f', '33'], {
coerce: {
f: function (arg) {
return arg * -1
}
},
alias: {
f: ['foo']
}
})
parsed.f.should.deep.equal([-99, -33])
parsed.foo.should.deep.equal([-99, -33])
})

it('applies coercion function to an explicit array', function () {
var parsed = parser(['--foo', '99', '-f', '33'], {
coerce: {
f: function (arg) {
return arg.map(function (a) {
return a * -1
})
}
},
array: ['foo'],
alias: {
f: ['foo']
Expand All @@ -1996,6 +2013,19 @@ describe('yargs-parser', function () {
parsed.foo.should.deep.equal([-99, -33])
})

it('applies coercion function to _', function () {
var parsed = parser(['99', '33'], {
coerce: {
_: function (arg) {
return arg.map(function (a) {
return a * -1
})
}
}
})
parsed._.should.deep.equal([-99, -33])
})

// see: https://github.com/yargs/yargs/issues/550
it('coercion function can be used to parse large #s', function () {
var fancyNumberParser = function (arg) {
Expand All @@ -2014,7 +2044,7 @@ describe('yargs-parser', function () {
parsed.bar.should.equal(998)
})

it('populates argv.error, if an error is returned', function () {
it('populates argv.error, if an error is thrown', function () {
var parsed = parser.detailed(['--foo', '99'], {
coerce: {
foo: function (arg) {
Expand All @@ -2024,6 +2054,18 @@ describe('yargs-parser', function () {
})
parsed.error.message.should.equal('banana')
})

it('populates argv.error, if an error is thrown for an explicit array', function () {
var parsed = parser.detailed(['--foo', '99'], {
array: ['foo'],
coerce: {
foo: function (arg) {
throw Error('foo is array: ' + Array.isArray(arg))
}
}
})
parsed.error.message.should.equal('foo is array: true')
})
})

// see: https://github.com/yargs/yargs-parser/issues/37
Expand Down