-
Notifications
You must be signed in to change notification settings - Fork 21
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 isNumber function to util #79
Conversation
Thanks for bringing this to our attention. We could use ES6 |
no because isNaN('287349') is false and isNaN('foobar') is also false , which shouldn't be since '287349' is a string of a number and 'foobar' isn't. |
Apologies, I should have made it clear that ES6's Personally, I enforce strict type checking (via |
@MedoAngel After looking at the source again (it's been a while) I like the changes. It would be nice to get valid numbers from strings while rejecting invalid ones. Could we move the type checking to the entry function? I think it would be better to validate user input further up if possible. Line 67 in 8f87b9a
|
still returns false for normal strings, even if it returned true based on the type not even being a number that would disallow numeric strings from passing . so in summary using isNaN would give false for true and false for '234324' and true for 'foobar' while Number.isNaN will give false on all of them |
I understand and agree that numeric values (even strings) should be accepted. |
yes I agree will get right on it , would you like me to change the name as suggested to isNumeric ? and also add typeof checks to the function ? |
I agree that EDIT: Apologies, I just remembered languages can be called without the entry file. We would have to keep the number validation within the Apart from the function name change, this LGTM. |
@TylerVigario done, changed and pushed |
@MedoAngel What would happen if we use "3px" as input? Since we use It seems the type checking would pass for "3px" (since |
@TylerVigario you're absolutely right, idk how i missed it, will modify the code later and look into other places where Number is used in the code base and see if it would be better using parseFloat instead, or should i check for non numeric chars and exit early , this wouldn't allow 3px to even pass while parseFloat would give 3 |
Sounds good 👍 EDIT: I too wonder if we should allow "3px" and similar inputs. |
I think we shouldn't , this wouldn't be number to words this would be parsing text for numbers then convert to words |
@MedoAngel Agreed, I think it will just cause issues outside our scope like with decimals and money. |
@TylerVigario decimals should still be parsed since the lib support that, but symbols unrelated to numbers should be rejected |
@MedoAngel I meant how decimals take on a bit of a different meaning when used in currencies. So disallowing all text (including currency symbols) would certainly help steer users away from trying to the use the library for currencies (which it's not currently designed to handle). |
Yes Exactly |
@TylerVigario done with the changes we discussed earlier and added type checking just in case and for some speed improvement in case it's used on large data |
@MedoAngel Wouldn't it be more efficient to do this directly in if (value == NaN || typeof value != 'number') {
if (typeof value != 'string' || isNaN(value = Number(value))) {
throw new TypeError(`Invalid number: ${value}, of type: ${typeof value}`);
}
} That would take care of the boolean issue and reduce the need to check for text in the value. EDIT: Updated |
No because typeof NaN is number which returns true |
@MedoAngel Added check for EDIT: Hmm, nevermind, I guess that wouldn't work out as cleanly as I had thought. |
@TylerVigario Would still fail because '' is a string and Number('') is 0 so would return true |
True, however EDIT: I wonder what's the use-case for allowing string types at all? |
It does parse leading and trailing white space doesn't parse white space between two numbers which again isn't in our scope And i see that allowing strings would allow users to use the result directly from an input Edit: and it's not even about allowing string types , it's about giving a more safe test for the value which previously allowed strings, null , undefined ..etc |
I didn't know that but I am glad to hear and I agree that spaces between isn't within our scope.
We aren't dealing with user input as this is only a library. It's the responsibility of the application developer to ensure user input isn't malformed before passing it along. When we allow string types, we now have to check for thousands of things we don't allow.
It was already "safe" as it either inferred a number from the value or gave a EDIT: Improved grammar |
Sorry, should've made it clear what I mean by type safe, I don't mean that the program crashes, what I mean is that it doesn't accept illogical input like |
384e58f
to
161c754
Compare
@MedoAngel I totally agree that feeding n2words an empty string or However, it probably won't change much down the line for developers. For example; an application developer hooks their user input straight to this library without validating. Previously they'll get "zero" from an empty string but now it will throw a It seems to me the best way to reduce improper use is to disable string types. EDIT 4/24: This is probably a breaking change since previously handled illogical input will produce different results now. |
Resolved in #84. I wanted to merge this in but ultimately decided against it. I kept the logic within the |
this function is much safer than just using isNaN with Number because it rejects values like true, false,"" while Number accepts them as valid numbers