-
Notifications
You must be signed in to change notification settings - Fork 118
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
Add new type 'number' #5
Conversation
var n = parser([ '-n' ], { | ||
number: ['n'] | ||
}).n | ||
expect(n).to.equal(undefined) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nexdrew what are your thoughts about defaulting a number to undefined
... but one could also argue for NaN
or 0
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
defaulting to undefined
is better imo. Dealing with NaN
has always been a pain in the butt in my experience.
@maxrimue this is looking good so far, need any help getting tests passing? |
else if (flags.arrays && flags.arrays[key]) type = 'array' | ||
|
||
return type | ||
} | ||
|
||
function isNumber (x) { | ||
if (flags.numbers && flags.numbers[x] && !isNaN(x)) return true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can get rid of this check, it won't actually do anything, instead update:
https://github.com/yargs/yargs-parser/blob/master/index.js#L315
To be something like:
var value = val
if (!checkAllAliases(key, flags.strings)) {
if (isNumber(val) || checkAllAliases(key, flags.numbers)) value = Number(val)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This adds a new type
number
.A
number
-type option has the following special behaviour:undefined
, unless defined otherwise via.default()
NaN
(that means, ifNumber()
isn't capable of coercing the value to anumber
primitive)Note: Where should I implement the logic for checking if the given value is a number? I thought of the following code:
I am aware of
isNumber()
, however, it only returns true if the optionparse-numbers
equalsfalse
, which is obviously not related to thenumber
-type. What do folks think?