Skip to content
This repository was archived by the owner on Dec 18, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 19 additions & 0 deletions src/Global/Unsafe.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,22 @@
exports.unsafeStringify = function (x) {
return JSON.stringify(x);
};

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);
};
};

17 changes: 17 additions & 0 deletions src/Global/Unsafe.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,20 @@ module Global.Unsafe where
-- | Uses the global JSON object to turn anything into a string. Careful! Trying
-- | to serialize functions returns undefined
foreign import unsafeStringify :: forall a. a -> String

-- | 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

-- | 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
24 changes: 22 additions & 2 deletions test/Test/Main.purs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
module Test.Main where

import Prelude (Unit, (==), ($), bind, not, negate, (<), (>), (/=))
import Prelude

import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)

import Global
import Global (readFloat, readInt, isFinite, infinity, nan, isNaN)
import Global.Unsafe (unsafeToPrecision, unsafeToExponential, unsafeToFixed)

import Test.Assert (ASSERT, assert)

main :: Eff (console :: CONSOLE, assert :: ASSERT) Unit
main = do
let num = 12345.6789

log "nan /= nan"
assert $ nan /= nan
Expand Down Expand Up @@ -41,3 +43,21 @@ main = do

log "readFloat \"3.5\" == 3.5"
assert $ readFloat "3.5" == 3.5

-- note the rounding
log $ "unsafeToFixed 1" <> (show num) <> " == \"12345.7\""
assert $ unsafeToFixed 1 num == "12345.7"

-- padded with zeros
log $ "unsafeToFixed 6" <> (show num) <> " == \"12345.678900\""
assert $ unsafeToFixed 6 num == "12345.678900"

log $ "unsafeToExponential 4" <> (show num) <> " == \"1.2346e+4\""
assert $ unsafeToExponential 4 num == "1.2346e+4"

log $ "unsafeToPrecision 3" <> (show num) <> " == \"1.23e+4\""
assert $ unsafeToPrecision 3 num == "1.23e+4"

log $ "unsafeToPrecision 6" <> (show num) <> " == \"12345.7\""
assert $ unsafeToPrecision 6 num == "12345.7"