Skip to content

Dict

const Dict: object

Defined in: Data/Dict.ts:607

compact: <K, A>(dict) => ReadonlyMap<K, A>

Removes all None values from a dictionary, unwrapping Some values.

K

A

ReadonlyMap<K, Maybe<A>>

ReadonlyMap<K, A>

difference: <K, V>(other) => (dict) => ReadonlyMap<K, V>

Returns a new dictionary containing keys from dict that are not in other.

K

V

ReadonlyMap<K, V>

(dict) => ReadonlyMap<K, V>

empty: <K, V>() => ReadonlyMap<K, V>

Creates an empty dictionary.

K

V

ReadonlyMap<K, V>

const dict = Dict.empty<string, number>(); // ReadonlyMap {}

entries: <K, V>(dict) => readonly (readonly [K, V])[]

Returns all key-value pairs of a dictionary.

K

V

ReadonlyMap<K, V>

readonly (readonly [K, V])[]

  • keys for extracting dictionary keys.
  • values for extracting dictionary values.

filter: <A>(predicate) => <K>(dict) => ReadonlyMap<K, A>

Filters dictionary entries by a predicate on values.

A

(value) => boolean

<K>(dict) => ReadonlyMap<K, A>

filterWithKey for filtering with key access.

filterMap: <A, B>(transform) => <K>(dict) => ReadonlyMap<K, B>

Transforms values with a function returning Maybe, keeping only Some values.

A

B

(value) => Maybe<B>

<K>(dict) => ReadonlyMap<K, B>

filter for filtering without transformation.

filterWithKey: <K, A>(predicate) => (dict) => ReadonlyMap<K, A>

Filters dictionary entries by a predicate that also receives the key.

K

A

(key, value) => boolean

(dict) => ReadonlyMap<K, A>

filter for filtering values without key access.

from: object = DictFrom

entries: <K, V>(entries) => ReadonlyMap<K, V>

Creates a dictionary from an array of key-value tuples.

K

V

readonly (readonly [K, V])[]

ReadonlyMap<K, V>

Dict.from.entries([["a", 1], ["b", 2]]); // ReadonlyMap { "a" => 1, "b" => 2 }

nullable: <K, V>(dict) => Maybe<ReadonlyMap<K, V>>

Lifts a nullable dictionary into a Maybe. Returns None if null or undefined.

K

V

ReadonlyMap<K, V> | null | undefined

Maybe<ReadonlyMap<K, V>>

Dict.from.nullable(new Map([["a", 1]])); // Some(ReadonlyMap)
Dict.from.nullable(null);                 // None

record: <K, V>(record) => ReadonlyMap<K, V>

Creates a dictionary from a plain record.

K extends string

V

Readonly<Record<K, V>>

ReadonlyMap<K, V>

Dict.from.record({ a: 1, b: 2 }); // ReadonlyMap { "a" => 1, "b" => 2 }

groupBy: <A, K>(keySelector) => (items) => ReadonlyMap<K, NonEmptyArr<A>>

Groups elements of an array into a dictionary using a key selector function. Each key maps to a non-empty array of items that produced it.

A

K

(item) => K

(items) => ReadonlyMap<K, NonEmptyArr<A>>

pipe(["apple", "avocado", "banana"], Dict.groupBy(s => s[0]));
// ReadonlyMap { "a" => ["apple", "avocado"], "b" => ["banana"] }

has: <K>(key) => <V>(dict) => boolean

Returns true when the dictionary contains the specified key.

K

K

<V>(dict) => boolean

lookup for retrieving the value associated with a key.

insert: <K, V>(key, value) => (dict) => ReadonlyMap<K, V>

Returns a new dictionary with the key set to value.

K

V

K

V

(dict) => ReadonlyMap<K, V>

  • remove for deleting a key from a dictionary.
  • upsert for conditional insertion/update.

intersection: <K, V>(other) => (dict) => ReadonlyMap<K, V>

Returns a new dictionary containing only keys present in both dictionaries.

K

V

ReadonlyMap<K, V>

(dict) => ReadonlyMap<K, V>

  • union for combining all keys from both dictionaries.
  • difference for subtracting keys.

is: object = DictIs

empty: <K, V>(dict) => boolean

Returns true when the dictionary contains zero entries.

K

V

ReadonlyMap<K, V>

boolean

nonEmpty for checking if a dictionary contains entries.

nonEmpty: <K, V>(dict) => dict is NonEmptyMap<K, V>

Returns true when the dictionary contains at least one entry.

K

V

ReadonlyMap<K, V>

dict is NonEmptyMap<K, V>

empty for checking if a dictionary is empty.

keys: <K, V>(dict) => readonly K[]

Returns all keys of a dictionary.

K

V

ReadonlyMap<K, V>

readonly K[]

  • values for extracting dictionary values.
  • entries for extracting key-value pairs.

lookup: <K>(key) => <V>(dict) => Maybe<V>

Retrieves the value associated with a key wrapped in a Maybe.

K

K

<V>(dict) => Maybe<V>

has for checking key existence without retrieving the value.

map: <A, B>(transform) => <K>(dict) => ReadonlyMap<K, B>

Transforms each value in the dictionary.

A

B

(value) => B

<K>(dict) => ReadonlyMap<K, B>

mapWithKey for transforming values with key access.

mapEntries: <K1, V1, K2, V2>(transform) => (dict) => ReadonlyMap<K2, V2>

Transforms key and value pairs simultaneously into a new dictionary.

K1

V1

K2

V2

(key, value) => readonly [K2, V2]

(dict) => ReadonlyMap<K2, V2>

pipe(
  Dict.from.entries([["a", 1]]),
  Dict.mapEntries((k, v) => [k.toUpperCase(), v * 10])
); // ReadonlyMap { "A" => 10 }

mapKeys: <K1, K2, V>(transform) => (dict) => ReadonlyMap<K2, V>

Transforms dictionary keys while preserving values.

K1

K2

V

(key) => K2

(dict) => ReadonlyMap<K2, V>

pipe(
  Dict.from.entries([["a", 1]]),
  Dict.mapKeys(k => k.toUpperCase())
); // ReadonlyMap { "A" => 1 }

mapWithKey: <K, A, B>(transform) => (dict) => ReadonlyMap<K, B>

Transforms each value in the dictionary, also receiving the key.

K

A

B

(key, value) => B

(dict) => ReadonlyMap<K, B>

map for transforming values without key access.

mergeWith: <V>(combine) => {<K>(first, second): ReadonlyMap<K, V>; <K>(second): (first) => ReadonlyMap<K, V>; }

Merges two dictionaries using a combination function on collisions. Supports both uncurried (first, second) and curried (second)(first) invocation.

V

(first, second) => V

{<K>(first, second): ReadonlyMap<K, V>; <K>(second): (first) => ReadonlyMap<K, V>; }

const mergeAdd = Dict.mergeWith((a: number, b: number) => a + b);
const d1 = Dict.from.entries([["a", 1], ["b", 2]]);
const d2 = Dict.from.entries([["b", 3], ["c", 4]]);
mergeAdd(d1, d2); // ReadonlyMap { "a" => 1, "b" => 5, "c" => 4 }
pipe(d1, mergeAdd(d2)); // ReadonlyMap { "a" => 1, "b" => 5, "c" => 4 }

NonEmpty: object = DictNonEmptyConst

Utilities for working with guaranteed non-empty dictionaries (NonEmptyMap).

entries: <K, V>(dict) => NonEmptyArr<readonly [K, V]> = _nonEmptyEntries

Returns all entries of a non-empty dictionary as a NonEmptyArr.

K

V

NonEmptyMap<K, V>

NonEmptyArr<readonly [K, V]>

Dict.NonEmpty.entries(Dict.NonEmpty.singleton("a", 1)); // [["a", 1]]

from: object

map: <K, V>(dict) => Maybe<NonEmptyMap<K, V>> = _nonEmptyFromMap

Refines a standard Map to a NonEmptyMap, returning Some if size > 0, None otherwise.

K

V

ReadonlyMap<K, V>

Maybe<NonEmptyMap<K, V>>

Dict.NonEmpty.from.map(new Map([["a", 1]])); // Some(NonEmptyMap)
Dict.NonEmpty.from.map(new Map());           // None

keys: <K, V>(dict) => NonEmptyArr<K> = _nonEmptyKeys

Returns all keys of a non-empty dictionary as a NonEmptyArr.

K

V

NonEmptyMap<K, V>

NonEmptyArr<K>

Dict.NonEmpty.keys(Dict.NonEmpty.singleton("a", 1)); // ["a"]

map: <A, B>(transform) => <K>(dict) => NonEmptyMap<K, B> = _nonEmptyMap

Transforms each value of a non-empty dictionary, preserving non-emptiness.

A

B

(value) => B

<K>(dict) => NonEmptyMap<K, B>

pipe(Dict.NonEmpty.singleton("a", 1), Dict.NonEmpty.map(n => n * 2)); // NonEmptyMap { "a" => 2 }

mapWithKey: <K, A, B>(transform) => (dict) => NonEmptyMap<K, B> = _nonEmptyMapWithKey

Transforms each value with key access, preserving non-emptiness.

K

A

B

(key, value) => B

(dict) => NonEmptyMap<K, B>

pipe(Dict.NonEmpty.singleton("a", 1), Dict.NonEmpty.mapWithKey((k, v) => `${k}:${v}`));
// NonEmptyMap { "a" => "a:1" }

reduce: <V>(reducer) => <K>(dict) => V = _nonEmptyReduce

Reduces values of a non-empty dictionary using the first value as initial accumulator.

V

(accumulator, value) => V

<K>(dict) => V

pipe(Dict.NonEmpty.singleton("a", 5), Dict.NonEmpty.reduce((a, b) => a + b)); // 5

singleton: <K, V>(key, value) => NonEmptyMap<K, V> = _nonEmptySingleton

Creates a NonEmptyMap containing a single entry.

K

V

K

V

NonEmptyMap<K, V>

Dict.NonEmpty.singleton("a", 1); // NonEmptyMap { "a" => 1 }

values: <K, V>(dict) => NonEmptyArr<V> = _nonEmptyValues

Returns all values of a non-empty dictionary as a NonEmptyArr.

K

V

NonEmptyMap<K, V>

NonEmptyArr<V>

Dict.NonEmpty.values(Dict.NonEmpty.singleton("a", 1)); // [1]

reduce: <B, V>(initial, reducer) => <K>(dict) => B

Reduces dictionary values from an initial value.

B

V

B

(accumulator, value) => B

<K>(dict) => B

pipe(Dict.from.entries([["a", 1], ["b", 2]]), Dict.reduce(0, (acc, n) => acc + n)); // 3

reduceWithKey: <B, K, V>(initial, reducer) => (dict) => B

Reduces dictionary entries from an initial value, passing value and key to the reducer.

B

K

V

B

(accumulator, value, key) => B

(dict) => B

pipe(Dict.from.entries([["a", 1], ["b", 2]]), Dict.reduceWithKey("", (acc, v, k) => `${acc}${k}${v}`)); // "a1b2"

remove: <K>(key) => <V>(dict) => ReadonlyMap<K, V>

Returns a new dictionary with the specified key removed.

K

K

<V>(dict) => ReadonlyMap<K, V>

insert for adding or replacing a key in a dictionary.

singleton: <K, V>(key, value) => ReadonlyMap<K, V>

Creates a dictionary with a single key-value entry.

K

V

K

V

ReadonlyMap<K, V>

Dict.singleton("key", 42); // ReadonlyMap { "key" => 42 }

size: <K, V>(dict) => number

Returns the number of entries in the dictionary.

K

V

ReadonlyMap<K, V>

number

Dict.size(Dict.singleton("a", 1)); // 1

to: object = DictTo

record: <K, V>(dict) => Readonly<Record<K, V>>

Converts a dictionary to a plain record. Protects against prototype pollution when keys equal __proto__.

K extends string

V

ReadonlyMap<K, V>

Readonly<Record<K, V>>

Dict.to.record(Dict.from.entries([["a", 1], ["b", 2]])); // { a: 1, b: 2 }

union: <K, V>(other) => (dict) => ReadonlyMap<K, V>

Combines two dictionaries, preferring entries from other on key collisions.

K

V

ReadonlyMap<K, V>

(dict) => ReadonlyMap<K, V>

upsert: <K, V>(key, update) => (dict) => ReadonlyMap<K, V>

Inserts or updates a key using a callback that receives the existing value if present.

K

V

K

(existing) => V

(dict) => ReadonlyMap<K, V>

insert for unconditional key setting.

values: <K, V>(dict) => readonly V[]

Returns all values of a dictionary.

K

V

ReadonlyMap<K, V>

readonly V[]

  • keys for extracting dictionary keys.
  • entries for extracting key-value pairs.