Interface IMapImmutable<K, V>

An immutable map. Rather than changing the map, functions like add and delete return a new map reference which must be captured.

Immutable data is useful because as it gets passed around your code, it never changes from underneath you. You have what you have.

let m = map(); // Create
let m2 = m.set(`hello`, `samantha`);
// m is still empty, only m2 contains a value.
interface IMapImmutable<K, V> {
    add(...itemsToAdd: EitherKey<K, V>): IMapImmutable<K, V>;
    clear(): IMapImmutable<K, V>;
    delete(key: K): IMapImmutable<K, V>;
    entries(): IterableIterator<readonly [K, V]>;
    get(key: K): undefined | V;
    has(key: K): boolean;
    isEmpty(): boolean;
    set(key: K, value: V): IMapImmutable<K, V>;
    values(): IterableIterator<V>;
}

Type Parameters

  • K

    Type of map keys. Typically string

  • V

    Type of stored values

Hierarchy

  • IMapBase<K, V>
    • IMapImmutable

Methods

  • Adds one or more items, returning the changed map.

    Can add items in the form of [key,value] or {key, value}.

    Parameters

    Returns IMapImmutable<K, V>

    map.set(`hello`, `samantha`);
    map.add([`hello`, `samantha`]);
    map.add({key: `hello`, value: `samantha`})
  • Iterates over entries (consisting of [key,value])

    Returns IterableIterator<readonly [K, V]>

    for (const [key, value] of map.entries()) {
    // Use key, value...
    }
  • Gets an item by key

    Parameters

    • key: K

    Returns undefined | V

    const item = map.get(`hello`);
    
  • Returns true if map contains key

    Parameters

    • key: K

    Returns boolean

    if (map.has(`hello`)) ...
    
  • Returns true if map is empty

    Returns boolean

  • Sets key to be value, overwriting anything existing. Returns a new map with added key.

    Parameters

    • key: K
    • value: V

    Returns IMapImmutable<K, V>

  • Returns IterableIterator<V>