diff --git a/bower.json b/bower.json index 106c1aa..0d57cd8 100644 --- a/bower.json +++ b/bower.json @@ -2,13 +2,13 @@ "name": "purescript-numbers", "description": "Functions for working with Numbers", "license": "MIT", - "homepage": "https://github.com/sharkdp/purescript-numbers", + "homepage": "https://github.com/purescript/purescript-numbers", "authors": [ "David Peter " ], "repository": { "type": "git", - "url": "git://github.com/sharkdp/purescript-numbers.git" + "url": "git://github.com/purescript/purescript-numbers.git" }, "ignore": [ "**/.*", @@ -17,8 +17,8 @@ ], "dependencies": { "purescript-math": "master", - "purescript-globals": "master", - "purescript-maybe": "master" + "purescript-maybe": "master", + "purescript-functions": "master" }, "devDependencies": { "purescript-console": "master", diff --git a/src/Data/Number.js b/src/Data/Number.js new file mode 100644 index 0000000..16cbe4e --- /dev/null +++ b/src/Data/Number.js @@ -0,0 +1,19 @@ +/* globals exports */ +"use strict"; + +exports.nan = NaN; + +exports.isNaN = isNaN; + +exports.infinity = Infinity; + +exports.isFinite = isFinite; + +exports.fromStringImpl = function(str, isFinite, just, nothing) { + var num = parseFloat(str); + if (isFinite(num)) { + return just(num); + } else { + return nothing; + } +}; diff --git a/src/Data/Number.purs b/src/Data/Number.purs index 5706be3..3c6a587 100644 --- a/src/Data/Number.purs +++ b/src/Data/Number.purs @@ -7,10 +7,20 @@ module Data.Number , isFinite ) where -import Prelude - +import Data.Function.Uncurried (Fn4, runFn4) import Data.Maybe (Maybe(..)) -import Global as G + +-- | Not a number (NaN) +foreign import nan :: Number + +-- | Test whether a number is NaN +foreign import isNaN :: Number -> Boolean + +-- | Positive infinity +foreign import infinity :: Number + +-- | Test whether a number is finite +foreign import isFinite :: Number -> Boolean -- | Attempt to parse a `Number` using JavaScripts `parseFloat`. Returns -- | `Nothing` if the parse fails or if the result is not a finite number. @@ -40,23 +50,6 @@ import Global as G -- | (Just 1.2) -- | ``` fromString :: String -> Maybe Number -fromString = G.readFloat >>> check - where - check num | isFinite num = Just num - | otherwise = Nothing - --- | Not a number (NaN). -nan :: Number -nan = G.nan - --- | Test whether a `Number` is NaN. -isNaN :: Number -> Boolean -isNaN = G.isNaN - --- | Positive infinity. -infinity :: Number -infinity = G.infinity +fromString str = runFn4 fromStringImpl str isFinite Just Nothing --- | Test whether a number is finite. -isFinite :: Number -> Boolean -isFinite = G.isFinite +foreign import fromStringImpl :: Fn4 String (Number -> Boolean) (forall a. a -> Maybe a) (forall a. Maybe a) (Maybe Number) diff --git a/test/Main.purs b/test/Test/Main.purs similarity index 88% rename from test/Main.purs rename to test/Test/Main.purs index f6387ee..ca3f97d 100644 --- a/test/Main.purs +++ b/test/Test/Main.purs @@ -3,16 +3,49 @@ module Test.Main where import Prelude import Data.Maybe (Maybe(..), fromMaybe) -import Data.Number (nan, isNaN, infinity, isFinite, fromString) -import Data.Number.Format (precision, fixed, exponential, toStringWith, - toString) -import Data.Number.Approximate (Fraction(..), Tolerance(..), eqRelative, - eqAbsolute, (≅), (≇)) - import Effect (Effect) import Effect.Console (log) -import Test.Assert (assertTrue', assertFalse', assertEqual) +import Data.Number (isFinite, infinity,nan, isNaN, fromString) +import Data.Number.Format (precision, fixed, exponential, toStringWith, toString) +import Data.Number.Approximate (Fraction(..), Tolerance(..), eqRelative, eqAbsolute, (≅), (≇)) + +import Test.Assert (assert, assertTrue', assertFalse', assertEqual) + +main :: Effect Unit +main = do + globalsTestCode + numbersTestCode + +-- Test code for the `purescript-globals` repo before its' Number-related +-- code was moved into this repo +globalsTestCode :: Effect Unit +globalsTestCode = do + let num = 12345.6789 + + log "nan /= nan" + assert $ nan /= nan + + log "not (isNaN 6.0)" + assert $ not (isNaN 6.0) + + log "isNaN nan" + assert $ isNaN nan + + log "infinity > 0.0" + assert $ infinity > 0.0 + + log "-infinity < 0.0" + assert $ -infinity < 0.0 + + log "not (isFinite infinity)" + assert $ not (isFinite infinity) + + log "isFinite 0.0" + assert $ isFinite 0.0 + +-- Test code originally in this repo before parts of deprecated +-- `purescript-globals` repo was moved to this repo. -- | Comparison up to 10% relative error. eqRelative' :: Number -> Number -> Boolean @@ -26,8 +59,8 @@ eqAbsolute' = eqAbsolute (Tolerance 0.1) infix 1 eqAbsolute' as =~= -main :: Effect Unit -main = do +numbersTestCode :: Effect Unit +numbersTestCode = do log "Data.Number.fromString"