-
Notifications
You must be signed in to change notification settings - Fork 17
Add fromString
method
#4
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
Conversation
The Data.Int.fromString equivalent for Number. Tests have been provided, as well as code documentation.
Thanks you very much! This looks very good. I'll add a few inline comments. |
-> Maybe Number | ||
|
||
|
||
-- | Attempt to parse a Number from a String using JavaScript's parseFloat. |
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.
We should probably specify when to expect a Nothing
result here.
return function (string) { | ||
var result = parseFloat(string); | ||
|
||
return isNaN(result) ? nothing |
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 believe isNaN
is not enough here, because parseFloat
can also return Infinity
(for inputs like 1e1000
). isFinite
would be a better choice, I think.
Thinking about this, it would be very cool to have isNaN
and isFinite
available in purescript-numbers. Then, fromString
could also be implemented in PureScript.
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.
Agreed on Infinity
, will amend now. Interestingly, PureScript's core libraries tend to take the stance that NaN/Infinity is "undefined" behaviour. I wonder what @paf31 would think of isNaN
and isFinite
existing in PS.
test "invalid number string" do | ||
assert "invalid strings are not coerced" $ | ||
fromMaybe true $ false <$ fromString "bad string" | ||
|
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.
Thanks for adding tests, too! Could you also add a test for an input like 1e1000
?
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.
Yep, will do :)
Going to close this PR as I think that, aside from the edge checks, this is all already implemented in |
I knew that, but I thought that your version was still very useful.. precisely due to the checks. Something similar was also asked for in purescript-deprecated/purescript-globals#5
Hm, .. we could still use the unsafe
Yes, but there are situations where we cannot avoid getting |
See purescript/purescript-integers#17 for another related discussion. |
I went ahead, merged this and re-implemented it using the methods in Again, thank you very much! |
Oh, wow! Sorry, I'd gone to bed, hah! No problem - thank you very much, too! |
Hello!
This is the
Number
equivalent ofData.Int.fromString
. Hope this is ok! I've added tests to the suite, too.PS: I looked at the process of re-uploading packages, and, while I think I probably can do it, it would result in me being registered as the package uploader. Not quite sure what the repercussions are, so I'm going to run away and leave it to you. Probably being silly, but best to be safe.