Elmish.Foreign
- Package
- purescript-elmish
- Repository
- collegevine/purescript-elmish
#CanPassToJavaScript Source
class CanPassToJavaScript (a :: Type)
This class is used to assert that values of a type can be passed to JavaScript code directly (without conversion) and understood by that code. Specifically, this class is defined for primitives (strings, numbers, booleans), arrays, and records. This assertion is used in a number of places that pass complex values to JS code to restrict the set of types that can be safely passed.
It is still possible to define instances of this class for other,
non-primitive types, but you have to know what you're doing and make sure
that JS representation is sane and stable. For example, a common trick is
to newtype
-wrap known JS enumerations to provide type safety:
module HTMLButton
( ButtonType -- NOTE: not exporting the constructor
, typeButton, typeSubmit, typeReset
, ButtonProps, button
)
where
newtype ButtonType = ButtonType String
derive newtype instance CanPassToJavaScript ButtonType
typeButton = ButtonType "button" :: ButtonType
typeSubmit = ButtonType "submit" :: ButtonType
typeReset = ButtonType "reset" :: ButtonType
type ButtonProps =
{ type :: ButtonType
, ...
}
foreign import button :: ButtonProps -> ReactElement
Instances
CanPassToJavaScript Json
CanPassToJavaScript Foreign
CanPassToJavaScript String
CanPassToJavaScript Number
CanPassToJavaScript Boolean
CanPassToJavaScript JSDate
CanPassToJavaScript Int
(CanPassToJavaScript a) => CanPassToJavaScript (Object a)
CanPassToJavaScript (Effect Unit)
(CanPassToJavaScript a) => CanPassToJavaScript (Effect a)
(CanReceiveFromJavaScript a) => CanPassToJavaScript (EffectFn1 a Unit)
(CanReceiveFromJavaScript a, CanPassToJavaScript b) => CanPassToJavaScript (EffectFn1 a b)
(CanReceiveFromJavaScript a) => CanPassToJavaScript (EffectFn2 a b Unit)
(CanReceiveFromJavaScript a, CanReceiveFromJavaScript b, CanPassToJavaScript c) => CanPassToJavaScript (EffectFn2 a b c)
(CanPassToJavaScript a) => CanPassToJavaScript (Array a)
(CanPassToJavaScript a) => CanPassToJavaScript (Nullable a)
(CanPassToJavaScript a) => CanPassToJavaScript (Opt a)
(CanPassToJavaScript a) => CanPassToJavaScript (Req a)
(RowToList r rl, CanPassToJavaScriptRecord rl) => CanPassToJavaScript (Record r)
(CanPassToJavaScript a) => CanPassToJavaScript (Foreign -> a)
#CanReceiveFromJavaScript Source
class CanReceiveFromJavaScript (a :: Type) where
This class is used to assert that values of a type can be passed from JavaScript to PureScript without any conversions. Specifically, this class is defined for primitives (strings, numbers, booleans), arrays, and records.
Members
validateForeignType :: Proxy a -> Foreign -> ValidationResult
Instances
CanReceiveFromJavaScript Foreign
CanReceiveFromJavaScript String
CanReceiveFromJavaScript Number
CanReceiveFromJavaScript Boolean
CanReceiveFromJavaScript JSDate
CanReceiveFromJavaScript Int
CanReceiveFromJavaScript (Object Foreign)
(CanReceiveFromJavaScript a) => CanReceiveFromJavaScript (Object a)
CanReceiveFromJavaScript (Effect Unit)
(CanPassToJavaScript a) => CanReceiveFromJavaScript (EffectFn1 a Unit)
(CanPassToJavaScript a, CanPassToJavaScript b) => CanReceiveFromJavaScript (EffectFn2 a b Unit)
CanReceiveFromJavaScript (Array Foreign)
(CanReceiveFromJavaScript a) => CanReceiveFromJavaScript (Array a)
(CanReceiveFromJavaScript a) => CanReceiveFromJavaScript (Nullable a)
(CanReceiveFromJavaScript a) => CanReceiveFromJavaScript (Opt a)
(CanReceiveFromJavaScript a) => CanReceiveFromJavaScript (Req a)
(RowToList r rl, CanReceiveFromJavaScriptRecord rl) => CanReceiveFromJavaScript (Record r)
#readForeign Source
readForeign :: forall a. CanReceiveFromJavaScript a => Foreign -> Maybe a
Verifies if the given raw JS value is of the right type/shape to be
represented as a
, and if so, coerces the value to a
.
#readForeign' Source
readForeign' :: forall a. CanReceiveFromJavaScript a => Foreign -> Either String a
Verifies if the given raw JS value is of the right type/shape to be
represented as a
, and if so, coerces the value to a
.
#showForeign Source
showForeign :: Foreign -> String
#Arguments Source
data Arguments
Type of the arguments
object in a JS function (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments).
#argumentsToArray_ Source
argumentsToArray_ :: Arguments -> Array Foreign
Creates a new Array
from an Arguments
object.
#getArgument Source
getArgument :: Arguments -> Int -> Maybe Foreign
Gets the value at a specified index of an Arguments
object.
Returns Nothing
if there are not enough arguments.
#mkVarArgEff_ Source
mkVarArgEff_ :: (Arguments -> Effect Unit) -> Foreign
Creates a JS function that takes a variable number of args (via
arguments
) and calls the provided effectful continuation, passing the
arguments as an array.
#CanReceiveFromJavaScriptRecord Source
class CanReceiveFromJavaScriptRecord :: RowList Type -> Constraint
class CanReceiveFromJavaScriptRecord (rowList :: RowList Type) where
This class is implementation of validateForeignType
for records. It
validates a given JS hash (aka "object") against a given type row that
represents a PureScript record, recursively calling
validateForeignType
for each field.
Members
validateJsRecord :: Proxy rowList -> Foreign -> ValidationResult
Instances
CanReceiveFromJavaScriptRecord Nil
(IsSymbol name, CanReceiveFromJavaScript a, CanReceiveFromJavaScriptRecord rl') => CanReceiveFromJavaScriptRecord (Cons name a rl')
#CanPassToJavaScriptRecord Source
class CanPassToJavaScriptRecord :: RowList Type -> Constraint
class CanPassToJavaScriptRecord (rowList :: RowList Type)
This class is implementation of CanPassToJavaScript
for records. It
simply iterates over all fields, checking that every field is of a type
that also has an instance of CanPassToJavaScript
.
Instances
CanPassToJavaScriptRecord Nil
(IsSymbol name, CanPassToJavaScript a, CanPassToJavaScriptRecord rl') => CanPassToJavaScriptRecord (Cons name a rl')
Re-exports from Foreign
#Foreign Source
data Foreign
A type for foreign data.
Foreign data is data from any external unknown or unreliable source, for which it cannot be guaranteed that the runtime representation conforms to that of any particular type.
Suitable applications of Foreign
are
- To represent responses from web services
- To integrate with external JavaScript libraries.