Skip to content

Commit b056d0a

Browse files
committed
Re-implement fromString in PureScript
1 parent b879e9e commit b056d0a

File tree

3 files changed

+8
-20
lines changed

3 files changed

+8
-20
lines changed

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"output"
1717
],
1818
"dependencies": {
19-
"purescript-math": "^2.0.0"
19+
"purescript-math": "^2.0.0",
20+
"purescript-globals": "^3.0.0"
2021
},
2122
"devDependencies": {
2223
"purescript-console": "^3.0.0",

src/Data/Number.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/Data/Number.purs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,7 @@ import Prelude
1616

1717
import Data.Maybe (Maybe(..))
1818
import Math (abs)
19-
20-
21-
foreign import fromStringImpl
22-
:: (forall a. a -> Maybe a)
23-
-> (forall a. Maybe a)
24-
-> String
25-
-> Maybe Number
19+
import Global (readFloat, isFinite)
2620

2721

2822
-- | Attempt to parse a Number from a String using JavaScript's parseFloat.
@@ -41,8 +35,11 @@ foreign import fromStringImpl
4135
-- | > fromString "1.2e4"
4236
-- | (Just 12000.0)
4337
-- | ```
44-
fromString :: String -> Maybe Number
45-
fromString = fromStringImpl Just Nothing
38+
fromString String Maybe Number
39+
fromString = readFloat >>> check
40+
where
41+
check num | isFinite num = Just num
42+
| otherwise = Nothing
4643

4744

4845
-- | A type alias for (small) numbers, typically in the range *[0:1]*.

0 commit comments

Comments
 (0)