Module Js.Undefined

Provide utilities around undefined

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

type t(+'a);

Local alias for 'a Js.undefined

module Js := Js__.Js_internal
let return: 'a => t('a);

Constructs a value of 'a Js.undefined containing a value of 'a

let testAny: 'a => bool;

Returns true if the given value is empty (undefined)

let empty: t('a);

The empty value, undefined

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

Bind the contained value using the given function

If 'a Js.undefined contains a value, that value is unwrapped, mapped to a 'b using the given function a' -> 'b, then wrapped back up and returned as 'b Js.undefined

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

Iterates over the contained value with the given function

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

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

Maps 'a option to 'a Js.undefined

Some a -> return a
None -> empty
let toOption: t('a) => option('a);

Maps 'a Js.undefined to 'a option

return a -> Some a
empty -> None