Coerce function returns option of type "number" as "string"#182
Coerce function returns option of type "number" as "string"#182bcoe merged 1 commit intoyargs:masterfrom
Conversation
bcoe
left a comment
There was a problem hiding this comment.
my concern with this patch is folks who are trying to return the string "55" from coerce, and instead end up with the value 55 because we automatically parse numbers.
What if, instead, we apply the type casting to a variable before passing it to coerce, and always use the value returned by coerce -- I think this would be less shocking.
It depends on the option type wether we parse or not. The current behavior (without coerce functions) is:
So if users define their option as The key question finally is whether the option type has precedence over the return type of the coerce function. |
Can you explain the bug to me that this solves for you in |
const parse = require('yargs-parser');
const args = parse('--retries 2 --retries 3', {
number: ['retries'],
coerce: {
'retries': v => Array.isArray(v) ? v.pop() : v
// 'retries': v => Number(Array.isArray(v) ? v.pop() : v)
},
configuration: {
'parse-numbers': true, // default
'duplicate-arguments-array': true // default
}
}
);
console.log(args); // { _: [], retries: '3' }In short: a This is not a critical bug for Mocha, so don't wait with cutting your new release. |
Description
closes #176
Description of Change
applyCoercions(): the return value of the coerce function is now checked by callingmaybeCoerceNumber(), wether it should be converted to type number.maybeCoerceNumber(): currently return values of coerce functions are not converted to type number.I deleted this restriction <== THIS HAS TO BE CONFIRMED