Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
800ea85
Update URL in bower.json file to purescript GH organization
JordanMartinez Oct 23, 2020
49f6da4
Remove `globals` as a dependency
JordanMartinez Oct 23, 2020
40b755c
Copy `Global.Unsafe` module from `globals` into this repo
JordanMartinez Oct 24, 2020
db8e0a8
Copy over Global.purs from `globals` but exclude URI-related code
JordanMartinez Oct 24, 2020
46366de
Port `fromString` from Number.purs into Gobal.purs
JordanMartinez Oct 24, 2020
0641b14
Replace original Data.Number.purs file with Global.purs file
JordanMartinez Oct 24, 2020
5ef669b
Copy test code from `globals` into this repo, minux URI-related tests
JordanMartinez Oct 24, 2020
078d554
Update globals' test file's imports
JordanMartinez Oct 24, 2020
f97cb7d
Distinguish globals test code from (incoming) original numbers test code
JordanMartinez Oct 24, 2020
6e15ba4
Move numbers original test file content into globals test file
JordanMartinez Oct 24, 2020
10b7f12
Rename numbers test code's `main` to `numbersTestCode`
JordanMartinez Oct 24, 2020
92f7de4
Merge imports in test code
JordanMartinez Oct 24, 2020
0eed5c0
Remove original repo's test/Main.purs file
JordanMartinez Oct 24, 2020
f5d5808
Use `readFloat` directly
JordanMartinez Oct 24, 2020
95b031f
Remove duplicate `fromString` export
JordanMartinez Oct 24, 2020
f78d26c
Remove 'readInt' from repo
JordanMartinez Nov 12, 2020
84a71e4
Remove `readFloat` while preserving `fromString` implementation
JordanMartinez Nov 12, 2020
30bea47
Remove toFixed, toPrecision, and toExponential from repo
JordanMartinez Nov 12, 2020
1ce7c59
Fix compiler warning: remove redundant import
JordanMartinez Nov 12, 2020
1d5f112
Remove unsafeToFixed, unsafeToPrecision, and unsafeToExponential from…
JordanMartinez Nov 12, 2020
6d7695a
Cleanup imports by putting them on one line
JordanMartinez Nov 12, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <mail@david-peter.de>"
],
"repository": {
"type": "git",
"url": "git://github.com/sharkdp/purescript-numbers.git"
"url": "git://github.com/purescript/purescript-numbers.git"
},
"ignore": [
"**/.*",
Expand All @@ -17,8 +17,8 @@
],
"dependencies": {
"purescript-math": "master",
"purescript-globals": "master",
"purescript-maybe": "master"
"purescript-maybe": "master",
"purescript-functions": "master"
},
"devDependencies": {
"purescript-console": "master",
Expand Down
19 changes: 19 additions & 0 deletions src/Data/Number.js
Original file line number Diff line number Diff line change
@@ -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;
}
};
37 changes: 15 additions & 22 deletions src/Data/Number.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
51 changes: 42 additions & 9 deletions test/Main.purs → test/Test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down