11-- | This module defines types for some global Javascript functions
22-- | and values.
3- module Global where
3+ module Global
4+ ( nan
5+ , isNaN
6+ , infinity
7+ , isFinite
8+ , readInt
9+ , readFloat
10+ , toFixed
11+ , toExponential
12+ , toPrecision
13+ , decodeURI
14+ , encodeURI
15+ , decodeURIComponent
16+ , encodeURIComponent
17+ ) where
18+
19+ import Prelude
20+ import Data.Function.Uncurried (Fn3 , Fn4 , runFn3 , runFn4 )
21+ import Data.Maybe (Maybe (..))
422
523-- | Not a number (NaN)
624foreign import nan :: Number
@@ -19,3 +37,57 @@ foreign import readInt :: Int -> String -> Number
1937
2038-- | Parse a floating point value from a `String`
2139foreign import readFloat :: String -> Number
40+
41+ foreign import _toFixed :: forall a . Fn4 (String -> a ) (String -> a ) Int Number a
42+
43+ foreign import _toExponential :: forall a . Fn4 (String -> a ) (String -> a ) Int Number a
44+
45+ foreign import _toPrecision :: forall a . Fn4 (String -> a ) (String -> a ) Int Number a
46+
47+ -- | Formats Number as a String with limited number of digits after the dot.
48+ -- | May return `Nothing` when specified number of digits is less than 0 or
49+ -- | greater than 20. See ECMA-262 for more information.
50+ toFixed :: Int -> Number -> Maybe String
51+ toFixed digits n = runFn4 _toFixed (const Nothing ) Just digits n
52+
53+ -- | Formats Number as String in exponential notation limiting number of digits
54+ -- | after the decimal dot. May return `Nothing` when specified number of
55+ -- | digits is less than 0 or greater than 20 depending on the implementation.
56+ -- | See ECMA-262 for more information.
57+ toExponential :: Int -> Number -> Maybe String
58+ toExponential digits n = runFn4 _toExponential (const Nothing ) Just digits n
59+
60+ -- | Formats Number as String in fixed-point or exponential notation rounded
61+ -- | to specified number of significant digits. May return `Nothing` when
62+ -- | precision is less than 1 or greater than 21 depending on the
63+ -- | implementation. See ECMA-262 for more information.
64+ toPrecision :: Int -> Number -> Maybe String
65+ toPrecision digits n = runFn4 _toPrecision (const Nothing ) Just digits n
66+
67+ foreign import _decodeURI :: forall a . Fn3 (String -> a ) (String -> a ) String a
68+
69+ foreign import _encodeURI :: forall a . Fn3 (String -> a ) (String -> a ) String a
70+
71+ foreign import _decodeURIComponent :: forall a . Fn3 (String -> a ) (String -> a ) String a
72+
73+ foreign import _encodeURIComponent :: forall a . Fn3 (String -> a ) (String -> a ) String a
74+
75+ -- | URI decoding. Returns `Nothing` when given a value with undecodeable
76+ -- | escape sequences.
77+ decodeURI :: String -> Maybe String
78+ decodeURI s = runFn3 _decodeURI (const Nothing ) Just s
79+
80+ -- | URI encoding. Returns `Nothing` when given a value with unencodeable
81+ -- | characters.
82+ encodeURI :: String -> Maybe String
83+ encodeURI s = runFn3 _encodeURI (const Nothing ) Just s
84+
85+ -- | URI component decoding. Returns `Nothing` when given a value with
86+ -- | undecodeable escape sequences.
87+ decodeURIComponent :: String -> Maybe String
88+ decodeURIComponent s = runFn3 _decodeURIComponent (const Nothing ) Just s
89+
90+ -- | URI component encoding. Returns `Nothing` when given a value with
91+ -- | unencodeable characters.
92+ encodeURIComponent :: String -> Maybe String
93+ encodeURIComponent s = runFn3 _encodeURIComponent (const Nothing ) Just s
0 commit comments