-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
exports.fromStringImpl = function (just) { | ||
return function (nothing) { | ||
return function (string) { | ||
var result = parseFloat(string); | ||
|
||
return isNaN(result) ? nothing | ||
: just(result); | ||
}; | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ module Data.Number | |
, eqApproximate | ||
, (~=) | ||
, (≅) | ||
, fromString | ||
, neqApproximate | ||
, (≇) | ||
, Precision | ||
|
@@ -13,8 +14,37 @@ module Data.Number | |
|
||
import Prelude | ||
|
||
import Data.Maybe (Maybe(..)) | ||
import Math (abs) | ||
|
||
|
||
foreign import fromStringImpl | ||
:: (forall a. a -> Maybe a) | ||
-> (forall a. Maybe a) | ||
-> String | ||
-> 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 commentThe reason will be displayed to describe this comment to others. Learn more. We should probably specify when to expect a |
||
-- | | ||
-- | Example: | ||
-- | ```purs | ||
-- | > fromString "123" | ||
-- | (Just 123.0) | ||
-- | | ||
-- | > fromString "12.34" | ||
-- | (Just 12.34) | ||
-- | | ||
-- | > fromString "1e4" | ||
-- | (Just 10000.0) | ||
-- | | ||
-- | > fromString "1.2e4" | ||
-- | (Just 12000.0) | ||
-- | ``` | ||
fromString :: String -> Maybe Number | ||
fromString = fromStringImpl Just Nothing | ||
|
||
|
||
-- | A type alias for (small) numbers, typically in the range *[0:1]*. | ||
type Fraction = Number | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,8 @@ module Test.Main where | |
|
||
import Prelude | ||
|
||
import Data.Number (eqRelative, eqAbsolute, (≅), (≇)) | ||
import Data.Maybe (fromMaybe) | ||
import Data.Number (eqRelative, eqAbsolute, fromString, (≅), (≇)) | ||
|
||
import Control.Monad.Aff.AVar (AVAR) | ||
import Control.Monad.Eff (Eff) | ||
|
@@ -119,6 +120,25 @@ main = runTest do | |
0.1 + 0.200001 ≇ 0.3 | ||
|
||
|
||
suite "fromString" do | ||
test "valid number string" do | ||
assert "integer strings are coerced" $ | ||
fromMaybe false $ map (_ == 123.0) $ fromString "123" | ||
|
||
assert "decimals are coerced" $ | ||
fromMaybe false $ map (_ == 12.34) $ fromString "12.34" | ||
|
||
assert "exponents are coerced" $ | ||
fromMaybe false $ map (_ == 1e4) $ fromString "1e4" | ||
|
||
assert "decimals exponents are coerced" $ | ||
fromMaybe false $ map (_ == 1.2e4) $ fromString "1.2e4" | ||
|
||
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 commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, will do :) |
||
|
||
suite "eqAbsolute" do | ||
test "eqAbsolute" do | ||
assert "should succeed for differences smaller than the precision" $ | ||
|
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, becauseparseFloat
can also returnInfinity
(for inputs like1e1000
).isFinite
would be a better choice, I think.Thinking about this, it would be very cool to have
isNaN
andisFinite
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 ofisNaN
andisFinite
existing in PS.