API / JavaScript / Js / Dict

Dict

Provide utilities for JS dictionary object.

type t('a);

Dictionary type (ie an '{ }' JS object). However it is restricted to hold a single type; therefore values must have the same type. This Dictionary type is mostly used with the Js_json.t type.

type key = string;

Key type.

let get: (t('a), key) => option('a);

Js.Dict.get(key) returns None if the key is not found in the dictionary, Some(value) otherwise.

let unsafeGet: (t('a), key) => 'a;

Js.Dict.unsafeGet(key) returns the value if the key exists, otherwise an undefined value is returned. Must be used only when the existence of a key is certain. (i.e. when having called keys function previously).

RE
let dict = Js.Dict.fromList([("first", "one"), ("second", "two"), ("third", "three")]); Array.iter(key => Js.log(Js.Dict.unsafeGet(dict, key)), Js.Dict.keys(dict)); /* "one" */ /* "two" */ /* "three" */
let set: (t('a), key, 'a) => unit;

set(dict, key, value) sets the key/value in the dictionary dict.

let keys: t('a) => array(string);

keys(dict) returns all the keys in the dictionary dict.

let empty: unit => t('a);

empty() returns an empty dictionary.

let unsafeDeleteKey: (. t(string), string) => unit;

Experimental internal function

let entries: t('a) => array((key, 'a));

entries(dict) returns the key value pairs in dict (ES2017).

let values: t('a) => array('a);

values(dict) returns the values in dict (ES2017).

let fromList: list((key, 'a)) => t('a);

fromList(entries) creates a new dictionary containing each (key, value) pair in the entries list.

let fromArray: array((key, 'a)) => t('a);

fromArray(entries) creates a new dictionary containing each (key, value) pair in the entries array.

let map: ((. 'a) => 'b, t('a)) => t('b);

map(f, dict) maps dict to a new dictionary with the same keys, using the function f to map each value.