-
Notifications
You must be signed in to change notification settings - Fork 417
Add isNumeric #122
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 isNumeric #122
Conversation
|
I'll look at this next week, Viktor. |
|
@MikeMcl ping |
|
Sorry, Viktor. Something came up. Thanks for the reminder. |
|
@MikeMcl It's fine :) Thanks for coming back! |
|
Looks good, but I'm not sure. In expected usage, it would mean that the regex test is executed twice, with the second test completely redundant: if (Big.isNumeric(value)) {
x = new Big(value);
}
In bignumber.js, I return a BigNumber with the value x = new BigNumber(value);
if (!x.isNaN()) {
...but here there is no Big One option is to remove the test from the Big constructor, and only include it in the x = new Big(2); // value not checked, and there is no need to check it
value = getValueFromSomewhere();
if (Big.isNumeric(value)) { // value checked explicitly
y = new Big(value);
} else { ...This puts the user in control of when a value gets tested, so unnnecessary checks can be avoided and hence the library becomes slightly more efficient. The advantage of your implementation though is that existing users could just continue to use the library as normal. Anyway, I prefer Any thoughts? |
It bothers me as well. I'd like to avoid it!
Our codebase is using
Yeah, it is an option. But it would introduce quite the breaking change, wouldn't? Will it require to introduce the default value?
You know what, maybe we can add second constructor that will initiate const n = new Big.Unsafe('Definitely not a numeric!') // what should happen it this case? 0?Unsafe constructor will simply mean that it leaves the check for the input to you, developer. Doing that we have backwards compatibility and not doing redundant second check.
I thought about |
|
@MikeMcl ping (exactly 11 days just like previous time) |
|
I have decided not to accept this. This is a minimalist library and the high bar to API additions has not quite been met. I am not convinced enough by the argument that a try & catch block is too expensive or too complex. Thanks for your efforts. |
Hi.
Sometimes it's very convenient to check the value before passing it to the Big constructor e.g. in case when throwing an error is fatal and try & catch block is too expensive or too complex.
Was thinking how to avoid throwing an error altogether, but couldn't come up with anything smart hence the static
isNumeric.Thanks