Safe Haskell | None |
---|---|
Language | Haskell2010 |
Deals with JavaScript values. These can be
- null
- undefined
- true | false
- a double precision floating point number
- a string
- an object
- data JSVal
- class ToJSVal a where
- data JSNull = JSNull
- type JSUndefined = ()
- data JSString
- data JSValue
- showJSValue :: JSValue -> String
- isTruthy :: JSVal -> GHCJSPure Bool
- valToBool :: ToJSVal value => value -> JSM Bool
- valToNumber :: ToJSVal value => value -> JSM Double
- valToStr :: ToJSVal value => value -> JSM JSString
- valToObject :: ToJSVal value => value -> JSM Object
- valToText :: ToJSVal value => value -> JSM Text
- valToJSON :: ToJSVal value => value -> JSM JSString
- val :: ToJSVal value => value -> JSM JSVal
- jsNull :: JSVal
- valNull :: JSVal
- isNull :: JSVal -> GHCJSPure Bool
- valIsNull :: ToJSVal value => value -> JSM Bool
- jsUndefined :: JSVal
- valUndefined :: JSVal
- isUndefined :: JSVal -> GHCJSPure Bool
- valIsUndefined :: ToJSVal value => value -> JSM Bool
- maybeNullOrUndefined :: ToJSVal value => value -> JSM (Maybe JSVal)
- maybeNullOrUndefined' :: ToJSVal value => (JSVal -> JSM a) -> value -> JSM (Maybe a)
- toJSBool :: Bool -> JSVal
- jsTrue :: JSVal
- jsFalse :: JSVal
- valBool :: Bool -> JSVal
- valMakeNumber :: Double -> JSM JSVal
- valMakeString :: JSString -> JSM JSVal
- valMakeText :: Text -> JSM JSVal
- valMakeJSON :: Value -> JSM JSVal
- deRefVal :: ToJSVal value => value -> JSM JSValue
- valMakeRef :: JSValue -> JSM JSVal
- strictEqual :: (ToJSVal a, ToJSVal b) => a -> b -> JSM Bool
- instanceOf :: (ToJSVal value, MakeObject constructor) => value -> constructor -> JSM Bool
JavaScript value references
Haskell types for JavaScript values
JSNull | Type that represents a value that can only be null. Haskell of course has no null so we are adding this type. |
type JSUndefined Source #
= () | A type that can only be undefined in JavaScript. Using ()
because functions in JavaScript that have no return, impicitly
return undefined.
type JSBool = Bool -- ^ JavaScript boolean values map the |
A wrapper around a JavaScript string
An algebraic data type that can represent a JavaScript value. Any JavaScriptCore
JSVal
can be converted into this type.
showJSValue :: JSValue -> String Source #
Show a JSValue but just say "object" if the value is a JavaScript object.
Converting JavaScript values
valToBool :: ToJSVal value => value -> JSM Bool Source #
Given a JavaScript value get its boolean value. All values in JavaScript convert to bool.
>>>
testJSaddle $ valToBool JSNull
false>>>
testJSaddle $ valToBool ()
false>>>
testJSaddle $ valToBool True
true>>>
testJSaddle $ valToBool False
false>>>
testJSaddle $ valToBool (1.0 :: Double)
true>>>
testJSaddle $ valToBool (0.0 :: Double)
false>>>
testJSaddle $ valToBool ""
false>>>
testJSaddle $ valToBool "1"
true
valToNumber :: ToJSVal value => value -> JSM Double Source #
Given a JavaScript value get its numeric value. May throw JSException.
>>>
testJSaddle $ show <$> valToNumber JSNull
0.0>>>
testJSaddle $ show <$> valToNumber ()
NaN>>>
testJSaddle $ show <$> valToNumber True
1.0>>>
testJSaddle $ show <$> valToNumber False
0.0>>>
testJSaddle $ show <$> valToNumber (1.0 :: Double)
1.0>>>
testJSaddle $ show <$> valToNumber (0.0 :: Double)
0.0>>>
testJSaddle $ show <$> valToNumber ""
0.0>>>
testJSaddle $ show <$> valToNumber "1"
1.0
valToStr :: ToJSVal value => value -> JSM JSString Source #
Given a JavaScript value get its string value (as a JavaScript string). May throw JSException.
>>>
testJSaddle $ strToText <$> valToStr JSNull
null>>>
testJSaddle $ strToText <$> valToStr ()
undefined>>>
testJSaddle $ strToText <$> valToStr True
true>>>
testJSaddle $ strToText <$> valToStr False
false>>>
testJSaddle $ strToText <$> valToStr (1.0 :: Double)
1>>>
testJSaddle $ strToText <$> valToStr (0.0 :: Double)
0>>>
testJSaddle $ strToText <$> valToStr ""
>>>
testJSaddle $ strToText <$> valToStr "1"
1
valToObject :: ToJSVal value => value -> JSM Object Source #
Given a JavaScript value get its object value. May throw JSException.
>>>
testJSaddle $ (valToObject JSNull >>= valToText) `catch` \ (JSException e) -> valToText e
null>>>
testJSaddle $ (valToObject () >>= valToText) `catch` \ (JSException e) -> valToText e
undefined>>>
testJSaddle $ valToObject True
true>>>
testJSaddle $ valToObject False
false>>>
testJSaddle $ valToObject (1.0 :: Double)
1>>>
testJSaddle $ valToObject (0.0 :: Double)
0>>>
testJSaddle $ valToObject ""
>>>
testJSaddle $ valToObject "1"
1
valToText :: ToJSVal value => value -> JSM Text Source #
Given a JavaScript value get its string value (as a Haskell Text
).
May throw JSException.
>>>
testJSaddle $ show <$> valToText JSNull
"null">>>
testJSaddle $ show <$> valToText ()
"undefined">>>
testJSaddle $ show <$> valToText True
"true">>>
testJSaddle $ show <$> valToText False
"false">>>
testJSaddle $ show <$> valToText (1.0 :: Double)
"1">>>
testJSaddle $ show <$> valToText (0.0 :: Double)
"0">>>
testJSaddle $ show <$> valToText ""
"">>>
testJSaddle $ show <$> valToText "1"
"1"
valToJSON :: ToJSVal value => value -> JSM JSString Source #
Given a JavaScript value get a JSON string value. May throw JSException.
>>>
testJSaddle $ strToText <$> valToJSON JSNull
null>>>
testJSaddle $ strToText <$> valToJSON ()
>>>
testJSaddle $ strToText <$> valToJSON True
true>>>
testJSaddle $ strToText <$> valToJSON False
false>>>
testJSaddle $ strToText <$> valToJSON (1.0 :: Double)
1>>>
testJSaddle $ strToText <$> valToJSON (0.0 :: Double)
0>>>
testJSaddle $ strToText <$> valToJSON ""
"">>>
testJSaddle $ strToText <$> valToJSON "1"
"1">>>
testJSaddle $ strToText <$> (obj >>= valToJSON)
{}
Make JavaScript values from Haskell ones
Convert to a JavaScript value (just an alias for toJSVal
)
valIsNull :: ToJSVal value => value -> JSM Bool Source #
Test a JavaScript value to see if it is null
jsUndefined :: JSVal Source #
valUndefined :: JSVal Source #
An undefined
JavaScript value
valIsUndefined :: ToJSVal value => value -> JSM Bool Source #
Test a JavaScript value to see if it is undefined
maybeNullOrUndefined :: ToJSVal value => value -> JSM (Maybe JSVal) Source #
Convert a JSVal to a Maybe JSVal (converting null and undefined to Nothing)
Convert to and from JSValue
deRefVal :: ToJSVal value => value -> JSM JSValue Source #
Derefernce a value reference.
>>>
testJSaddle $ showJSValue <$> deRefVal JSNull
null>>>
testJSaddle $ showJSValue <$> deRefVal ()
undefined>>>
testJSaddle $ showJSValue <$> deRefVal True
true>>>
testJSaddle $ showJSValue <$> deRefVal False
false>>>
testJSaddle $ showJSValue <$> deRefVal (1.0 :: Double)
1.0>>>
testJSaddle $ showJSValue <$> deRefVal (0.0 :: Double)
0.0>>>
testJSaddle $ showJSValue <$> deRefVal ""
"">>>
testJSaddle $ showJSValue <$> deRefVal "1"
"1">>>
testJSaddle $ showJSValue <$> (valToObject True >>= deRefVal)
true>>>
testJSaddle $ showJSValue <$> (obj >>= deRefVal)
object
valMakeRef :: JSValue -> JSM JSVal Source #
Make a JavaScript value out of a JSValue
ADT.
>>>
testJSaddle $ valMakeRef ValNull
null>>>
testJSaddle $ valMakeRef ValUndefined
undefined>>>
testJSaddle $ valMakeRef (ValBool True)
true>>>
testJSaddle $ valMakeRef (ValNumber 1)
1>>>
testJSaddle $ valMakeRef (ValString $ T.pack "Hello")
Hello
instanceOf :: (ToJSVal value, MakeObject constructor) => value -> constructor -> JSM Bool Source #
Orphan instances
MakeArgs Bool Source # | Makes an argument list with just a single JavaScript boolean value |
MakeArgs Double Source # | Makes an argument list with just a single JavaScript number |
MakeArgs () Source # | This allows us to pass no arguments easily (altenative would be to use |
MakeArgs Text Source # | Makes an argument list with just a single JavaScript string |
MakeArgs Value Source # | Makes an argument list with just a single JSON value |
MakeArgs JSVal Source # | A single JSVal can be used as the argument list |
MakeObject JSVal Source # | |
FromJSVal Char Source # | |
FromJSVal Text Source # | |
FromJSVal JSString Source # | |
ToJSVal Bool Source # | Make a JavaScript boolean value |
ToJSVal Char Source # | |
ToJSVal Double Source # | Makes a JavaScript number |
ToJSVal Float Source # | |
ToJSVal Int Source # | |
ToJSVal Int8 Source # | |
ToJSVal Int16 Source # | |
ToJSVal Int32 Source # | |
ToJSVal Word Source # | |
ToJSVal Word8 Source # | |
ToJSVal Word16 Source # | |
ToJSVal Word32 Source # | |
ToJSVal Text Source # | Makes a JavaScript string |
ToJSVal Value Source # | Makes a JSON value |
ToJSVal JSVal Source # | If we already have a JSVal we are fine |
ToJSVal JSString Source # | Makes a JavaScript string |
ToJSVal Object Source # | |
ToJSVal JSUndefined Source # | Makes an |
FromJSString String Source # | |
FromJSString Text Source # | |
FromJSString JSString Source # | |
ToJSString String Source # | |
ToJSString Text Source # | |
ToJSString JSString Source # | If we already have a JSString we are fine |
FromJSVal a => FromJSVal [a] Source # | |
FromJSVal a => FromJSVal (Maybe a) Source # | |
ToJSVal a => ToJSVal [a] Source # | |
ToJSVal a => ToJSVal (Maybe a) Source # | Makes a JSVal or |
ToJSVal v => ToJSVal (JSM v) Source # | JSVal can be made by evaluating a function in |