-
Notifications
You must be signed in to change notification settings - Fork 476
Stricter number parsing #8129
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
base: master
Are you sure you want to change the base?
Stricter number parsing #8129
Conversation
…String for stricter parsing
for stricter number parsing
rescript
@rescript/darwin-arm64
@rescript/darwin-x64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
| let fromString: string => option<float> = %raw(`str => { | ||
| if (!str || !str.trim()) return; | ||
| let num = +str; | ||
| return isNaN(num) ? undefined : num; | ||
| }`) |
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 wrote some fast-check tests and couldn't find a difference between +str and Number(str), but the latter is easier to understand and could be written in pure ReScript.
How about converting this function to ReScript?
%%private(
@val external makeNumber: 'a => float = "Number"
@send external trim: string => string = "trim"
)
let fromString: string => option<float> = str =>
if Js.typeof(str) === "string" && trim(str) !== "" {
let num = makeNumber(str)
isNaN(num) ? None : Some
} else {
None
}BTW just saw this, but I don't think the performance difference between +x and Number(x) should be a factor in this decision. However, my tests show the difference is nearly non-existent:
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.
Yes, it's solely for performance, otherwise it should be equivalent. There might not be a noticeable difference when optimized anymore, but there are still situations where the might not be optimized and `Number would be significantly slower. If this is relevant here I'm not sure of though.
Another option is to just add a comment.
Fixes rescript-core/#257
Besides stricter number parsing, this also removes the radix argument in favor of supporting hexadecimal, binary and exponential notation directly.