-
Notifications
You must be signed in to change notification settings - Fork 18
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
Port Number
-related code from deprecated purescript-globals into this repo
#12
Changes from 15 commits
800ea85
49f6da4
40b755c
db8e0a8
46366de
0641b14
5ef669b
078d554
f97cb7d
6e15ba4
10b7f12
92f7de4
0eed5c0
f5d5808
95b031f
f78d26c
84a71e4
30bea47
1ce7c59
1d5f112
6d7695a
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,33 @@ | ||
/* globals exports */ | ||
"use strict"; | ||
|
||
exports.nan = NaN; | ||
|
||
exports.isNaN = isNaN; | ||
|
||
exports.infinity = Infinity; | ||
|
||
exports.isFinite = isFinite; | ||
|
||
exports.readInt = function (radix) { | ||
return function (n) { | ||
return parseInt(n, radix); | ||
}; | ||
}; | ||
|
||
exports.readFloat = parseFloat; | ||
|
||
var formatNumber = function (format) { | ||
return function (fail, succ, digits, n) { | ||
try { | ||
return succ(n[format](digits)); | ||
} | ||
catch (e) { | ||
return fail(e.message); | ||
} | ||
}; | ||
}; | ||
|
||
exports._toFixed = formatNumber("toFixed"); | ||
exports._toExponential = formatNumber("toExponential"); | ||
exports._toPrecision = formatNumber("toPrecision"); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,12 +5,34 @@ module Data.Number | |
, isNaN | ||
, infinity | ||
, isFinite | ||
, readInt | ||
, readFloat | ||
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. I don't think it makes sense to export this function, as it's just a less safe version of 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. Agreed. |
||
, toFixed | ||
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. I would rather not export 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. Agreed. |
||
, toExponential | ||
, toPrecision | ||
) 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 | ||
|
||
-- | Parse an integer from a `String` in the specified base | ||
foreign import readInt :: Int -> String -> Number | ||
|
||
-- | Parse a floating point value from a `String` | ||
foreign import readFloat :: String -> Number | ||
|
||
-- | 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 +62,33 @@ import Global as G | |
-- | (Just 1.2) | ||
-- | ``` | ||
fromString :: String -> Maybe Number | ||
fromString = G.readFloat >>> check | ||
fromString = readFloat >>> check | ||
where | ||
check num | isFinite num = Just num | ||
| otherwise = Nothing | ||
|
||
-- | Not a number (NaN). | ||
nan :: Number | ||
nan = G.nan | ||
foreign import _toFixed :: forall a. Fn4 (String -> a) (String -> a) Int Number a | ||
|
||
foreign import _toExponential :: forall a. Fn4 (String -> a) (String -> a) Int Number a | ||
|
||
foreign import _toPrecision :: forall a. Fn4 (String -> a) (String -> a) Int Number a | ||
|
||
-- | Test whether a `Number` is NaN. | ||
isNaN :: Number -> Boolean | ||
isNaN = G.isNaN | ||
-- | Formats Number as a String with limited number of digits after the dot. | ||
-- | May return `Nothing` when specified number of digits is less than 0 or | ||
-- | greater than 20. See ECMA-262 for more information. | ||
toFixed :: Int -> Number -> Maybe String | ||
toFixed digits n = runFn4 _toFixed (const Nothing) Just digits n | ||
|
||
-- | Positive infinity. | ||
infinity :: Number | ||
infinity = G.infinity | ||
-- | Formats Number as String in exponential notation limiting number of digits | ||
-- | after the decimal dot. May return `Nothing` when specified number of | ||
-- | digits is less than 0 or greater than 20 depending on the implementation. | ||
-- | See ECMA-262 for more information. | ||
toExponential :: Int -> Number -> Maybe String | ||
toExponential digits n = runFn4 _toExponential (const Nothing) Just digits n | ||
|
||
-- | Test whether a number is finite. | ||
isFinite :: Number -> Boolean | ||
isFinite = G.isFinite | ||
-- | Formats Number as String in fixed-point or exponential notation rounded | ||
-- | to specified number of significant digits. May return `Nothing` when | ||
-- | precision is less than 1 or greater than 21 depending on the | ||
-- | implementation. See ECMA-262 for more information. | ||
toPrecision :: Int -> Number -> Maybe String | ||
toPrecision digits n = runFn4 _toPrecision (const Nothing) Just digits n |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* globals exports, JSON */ | ||
"use strict"; | ||
|
||
exports.unsafeToFixed = function (digits) { | ||
return function (n) { | ||
return n.toFixed(digits); | ||
}; | ||
}; | ||
|
||
exports.unsafeToExponential = function (digits) { | ||
return function (n) { | ||
return n.toExponential(digits); | ||
}; | ||
}; | ||
|
||
exports.unsafeToPrecision = function (digits) { | ||
return function (n) { | ||
return n.toPrecision(digits); | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Data.Number.Unsafe where | ||
|
||
-- | Formats Number as a String with limited number of digits after the dot. | ||
-- | | ||
-- | May throw RangeError if the number of digits is not within the allowed range | ||
-- | (standard precision range is 0 to 20, but implementations may change it) | ||
foreign import unsafeToFixed :: Int -> Number -> 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. I'd prefer not to export these either, since they're more JS-specific in how they handle errors, whereas the core libraries ought to be backend-independent as far as is possible. 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. Agreed. Should this be moved to a separate repo outside of core? Or do you think it's fine to drop them entirely? 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. I think it's fine to drop them entirely: it's hard for me to imagine a situation where you'd want these functions to be able to throw. |
||
|
||
-- | Formats Number as String in exponential notation limiting number of digits | ||
-- | after the decimal dot. | ||
-- | | ||
-- | May throw RangeError if the number of digits is not within the allowed range | ||
-- | (standard precision range is 0 to 20, but implementations may change it) | ||
foreign import unsafeToExponential :: Int -> Number -> String | ||
|
||
-- | Formats Number as String in fixed-point or exponential notation rounded | ||
-- | to specified number of significant digits. | ||
-- | | ||
-- | May throw RangeError if the number of digits is not within the allowed range | ||
-- | (standard precision range is 0 to 100, but implementations may change it) | ||
foreign import unsafeToPrecision :: Int -> Number -> 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.
I don't think it makes to export
readInt
from this module, asreadInt
doesn't really have much to do with numbers. Additionally,Data.Int
already provides afromString
which does what we want.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.
While it does provide
fromString
,readInt
can parse aString
into a specified base. Should we port this function (albeit under a different name) topurescript-integers
? Or should it be dropped altogether?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.
That's a good point. I think we should just drop it for now; I think we can add integer parsing in a specified base to
purescript-integers
if and when someone asks for it.