@geraintguan/ts-std-lib
    Preparing search index...

    Class DefaultMap<TKey, TValue, THashedKey, TDefaultValue>

    Implementation of a specialised key-value map, based on map, that is created with a default value for retrieving keys that have not been set in the map.

    As this map is based of map it provides the same functionality such as the ability to customise the hashing function.

    When a default value is returned for a non-existent key, an entry is created for that key with the default value.

    const map = DefaultMap.empty<string, number>({
    defaultValue: { type: "value", value: 1 },
    });

    map.get("a"); // => 1

    map.set("b", 2);
    map.get("b"); // => 2

    [...map.keys()]; // => ["a", "b"]
    const map = DefaultMap.empty<string, number>({
    defaultValue: { type: "function", value: (key) => key.length },
    });

    map.get("cat"); // => 3
    map.get("cat-dog"); // => 7

    map.set("dog", 4);
    map.get("dog"); // => 4

    [...map.keys()]; // => ["cat", "cat-dog", "dog"]

    Type Parameters

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    _map: Map<THashedKey, TValue> = ...

    Internal data structure that we use to store the entries in the map.

    defaultValue:
        | { type: "function"; value: (key: TKey) => TDefaultValue }
        | { type: "value"; value: TDefaultValue }

    Default value to use in this map when accessing keys that do not exist.

    Two types of default value are supported:

    1. Providing a function will make the map call the given function that is called with the current access key to generate the default value.

    2. Providing a static value that will be used as-is as the default value.

    hash: (key: TKey) => THashedKey

    Function called to hash a key before storing or using it to access a value in the map.

    name: string = "unknown"

    Human readable name for the map instance that is used for debugging purposes.

    MissingKeyError: typeof HashMapMissingKeyError = HashMapMissingKeyError

    Error thrown when attempting to access a key in the map that does not exist.

    Methods

    • Clears all of the entries in this map.

      Returns void

      This function will mutate the map instance it is called on.

      const map = HashMap.fromEntries<number, string>([
      [1, "one"],
      [2, "two"],
      [3, "three"],
      ]);

      map.clear();

      [...map]; // => []
    • Delete an entry from this map by its key, throwing a HashMap.MissingKeyError if the key does not exist in this map.

      Parameters

      • key: TKey

        The key of the entry to delete.

      Returns void

      const map = HashMap.fromEntries<number, string>([
      [1, "one"],
      [2, "two"],
      [3, "three"],
      ]);

      map.delete(2);

      [...map];
      // => [
      // [1, "one"],
      // [3, "three"],
      // ]

      map.delete(4); // throws HashMap.MissingKeyError

      HashMap.MissingKeyError if the key does not exist in this map.

    • Delete an entry from this map by its key and returns true if it exists, otherwise it simply returns false.

      Parameters

      • key: TKey

        The key of the entry to delete.

      Returns boolean

      true if the key existed and the entry was deleted, false if the key did not exist.

      const map = HashMap.fromEntries<number, string>([
      [1, "one"],
      [2, "two"],
      [3, "three"],
      ]);

      map.deleteIfExists(2); // => true
      map.deleteIfExists(4); // => false

      [...map];
      // => [
      // [1, "one"],
      // [3, "three"],
      // ]
    • Returns the entries in this map as an IterableIterator which each entry as an array of [key, value].

      Returns IterableIterator<[THashedKey, TValue]>

      An IterableIterator of the entries in this map.

      const map = HashMap.fromEntries<number, string>([
      [1, "one"],
      [2, "two"],
      [3, "three"],
      ]);

      [...map.entries()];
      // => [
      // [1, "one"],
      // [2, "two"],
      // [3, "three"],
      // ]
    • Returns a new map with only the key-value pairs where the given function returns true, filtering out those that the given function returns false for.

      Parameters

      Returns DefaultMap<TKey, TValue, THashedKey, TDefaultValue>

      A new map with only the key-value pairs where the given function returned true for.

      The new map will have the same default value as this map.

      This function does not mutate the original map instance unless you call a mutating function on the map (4th argument) inside the given function.

      const map = HashMap.fromEntries<number, string>([
      [1, "one"],
      [2, "two"],
      [3, "three"],
      [4, "four"],
      ], {
      defaultValue: {
      type: "value",
      value: "NaN",
      }
      });

      const filtered = map.filter((value, key) => key > 2);

      [...filtered];
      // => [
      // [3, "three"],
      // [4, "four"],
      // ]
      const map = DefaultMap.fromEntries<number, string>([
      [1, "one"],
      [2, "two"],
      [3, "three"],
      [4, "four"],
      ], {
      defaultValue: {
      type: "value",
      value: "NaN",
      }
      });

      const filtered = map.filter((value) => value.length > 3);

      [...filtered];
      // => [
      // [3, "three"],
      // [4, "four"],
      // ]
    • Get the value with the given key from this map if it exists, if it doesn't return the default value configured for this map.

      Parameters

      • key: TKey

        The key of the value to get.

      Returns TValue | TDefaultValue

      The value with the given key in this map if it exists, if it doesn't then the default value configured for this map.

      Unlike HashMap.get, this method will not throw an error if the key does not exist in the map, instead returning the configured default value.

      Accessing an entry that does not exist with a key will set a new entry on the map with the given key and the created default value as the value of that entry.

    • Checks if this map has an entry with the given key.

      Parameters

      • key: TKey

        The key to check for.

      Returns boolean

      true if the key exists in this map, false otherwise.

    • Create a new map with entries that are the result of calling the given callback function on each of the key-value pairs in this map.

      Parameters

      Returns DefaultMap<TKey, TValue, THashedKey, TDefaultValue>

      A new map with the new entries from calling the given function on each key-value pair in the map.

      The new map will have the same default value as this map.

      This function does not mutate the original map instance unless you call a mutating function on the map (4th argument) inside the given function.

      const map = HashMap.fromEntries([
      ["one", 1],
      ["two", 2],
      ["three", 3],
      ], {
      defaultValue: {
      type: "value",
      value: "NaN",
      }
      });

      const swapped = map.map(([key, value]) => [value, key]);
      // => DefaultMap { 1 => "one", 2 => "two", 3 => "three" }
    • Set the value of an entry by it's key in the map, creating a new entry if one doesn't exist or overriding an existing entry if one does.

      Parameters

      • key: TKey

        The key of the entry to set.

      • value: TValue

        The value to set the entry to.

      Returns void

    • Get the values in this map as an IterableIterator.

      Returns IterableIterator<TValue>

      The values in this map as an IterableIterator.