Skip to content

Module Js.Undefined

Utility functions on undefined

Provides functionality for dealing with the 'a Js.undefinedJs.undefined('a) type

ocaml
type +'a t = 'a Js.undefined
reasonml
type t(+'a) = Js.undefined('a);

Local alias for 'a Js.undefinedJs.undefined('a)

ocaml
val return : 'a -> 'a t
reasonml
let return: 'a => t('a);

Constructs a value of 'a Js.undefinedJs.undefined('a) containing a value of 'a

ocaml
val testAny : 'a -> bool
reasonml
let testAny: 'a => bool;

Returns true if the given value is empty (undefined)

ocaml
val empty : 'a t
reasonml
let empty: t('a);

The empty value, undefined

ocaml
val getUnsafe : 'a t -> 'a
reasonml
let getUnsafe: t('a) => 'a;
ocaml
val getExn : 'a t -> 'a
reasonml
let getExn: t('a) => 'a;
ocaml
val map : f:('a -> 'b) Js.Fn.arity1 -> 'a t -> 'b t
reasonml
let map: f:Js.Fn.arity1(('a => 'b)) => t('a) => t('b);
ocaml
val bind : f:('a -> 'b t) Js.Fn.arity1 -> 'a t -> 'b t
reasonml
let bind: f:Js.Fn.arity1(('a => t('b))) => t('a) => t('b);

Bind the contained value using the given function

If 'a Js.undefinedJs.undefined('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.undefinedJs.undefined('b)

ocaml
let maybeGreetWorld (maybeGreeting: string Js.undefined) =
  Js.Undefined.bind maybeGreeting ~f:(fun greeting -> greeting ^ " world!")
reasonml
let maybeGreetWorld = (maybeGreeting: Js.undefined(string)) =>
  Js.Undefined.bind(maybeGreeting, ~f=greeting => greeting ++ " world!");
ocaml
val iter : f:('a -> unit) Js.Fn.arity1 -> 'a t -> unit
reasonml
let iter: f:Js.Fn.arity1(('a => unit)) => t('a) => unit;

Iterates over the contained value with the given function

If 'a Js.undefinedJs.undefined('a) contains a value, that value is unwrapped and applied to the given function.

ocaml
let maybeSay (maybeMessage: string Js.undefined) =
  Js.Undefined.iter maybeMessage ~f:(fun message -> Js.log message)
reasonml
let maybeSay = (maybeMessage: Js.undefined(string)) =>
  Js.Undefined.iter(maybeMessage, ~f=message => Js.log(message));
ocaml
val fromOption : 'a option -> 'a t
reasonml
let fromOption: option('a) => t('a);

Maps 'a optionoption('a) to 'a Js.undefinedJs.undefined('a)

| --- | --- | --- | | Some a | -> | return a | | None | -> | empty |

ocaml
val toOption : 'a t -> 'a option
reasonml
let toOption: t('a) => option('a);

Maps 'a Js.undefinedJs.undefined('a) to 'a optionoption('a)

| --- | --- | --- | | return a | -> | Some a | | empty | -> | None |