Module Js.Nullable
Utility functions on nullable
Contains functionality for dealing with values that can be both null and undefined
type +'a t = 'a Js.nullabletype t(+'a) = Js.nullable('a);Local alias for 'a Js.nullableJs.nullable('a)
val return : 'a -> 'a tlet return: 'a => t('a);Constructs a value of 'a Js.nullableJs.nullable('a) containing a value of 'a
val isNullable : 'a t -> boollet isNullable: t('a) => bool;Returns true if the given value is null or undefined, false otherwise
val null : 'a tlet null: t('a);The null value of type 'a Js.nullableJs.nullable('a)
val undefined : 'a tlet undefined: t('a);The undefined value of type 'a Js.nullableJs.nullable('a)
val map : f:('a -> 'b) Js.Fn.arity1 -> 'a t -> 'b tlet map: f:Js.Fn.arity1(('a => 'b)) => t('a) => t('b);val bind : f:('a -> 'b t) Js.Fn.arity1 -> 'a t -> 'b tlet bind: f:Js.Fn.arity1(('a => t('b))) => t('a) => t('b);Binds the contained value using the given function
If 'a Js.nullableJs.nullable('a) contains a value, that value is unwrapped, mapped to a 'b using the given function a' -> 'ba' => 'b, then wrapped back up and returned as 'b Js.nullableJs.nullable('b)
let maybeGreetWorld (maybeGreeting: string Js.nullable) =
Js.Nullable.bind maybeGreeting ~f:(fun greeting -> greeting ^ " world!")let maybeGreetWorld = (maybeGreeting: Js.nullable(string)) =>
Js.Nullable.bind(maybeGreeting, ~f=greeting => greeting ++ " world!");val iter : f:('a -> unit) Js.Fn.arity1 -> 'a t -> unitlet iter: f:Js.Fn.arity1(('a => unit)) => t('a) => unit;Iterates over the contained value with the given function
If 'a Js.nullableJs.nullable('a) contains a value, that value is unwrapped and applied to the given function.
let maybeSay (maybeMessage: string Js.nullable) =
Js.Nullable.iter maybeMessage ~f:(fun message -> Js.log message)let maybeSay = (maybeMessage: Js.nullable(string)) =>
Js.Nullable.iter(maybeMessage, ~f=message => Js.log(message));val fromOption : 'a option -> 'a tlet fromOption: option('a) => t('a);Maps 'a optionoption('a) to 'a Js.nullableJs.nullable('a)
| Some a | return a |
|---|---|
| None | undefined |
val toOption : 'a t -> 'a optionlet toOption: t('a) => option('a);Maps 'a Js.nullableJs.nullable('a) to 'a optionoption('a)
| return a | Some a |
|---|---|
| undefined | None |
| null | None |