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

    Class HashMap<TKey, TValue, THashedKey>

    Implementation of a key-value map that allows you to customise the way keys are stored by using a custom hashing function.

    Unlike an ES6 Map which always uses SameValueZero on the key stored as-is, map allows you to customise the way keys are stored by using a custom hashing function.

    By default this map uses identity as a hash function meaning that keys are stored as-is, which is the same behaviour as an ES6 Map.

    Type Parameters

    • TKey
    • TValue
    • THashedKey = TKey

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

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

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

    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 HashMap<TKey, TValue, THashedKey>

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

      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"],
      ]);

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

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

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

      [...filtered];
      // => [
      // [3, "three"],
      // [4, "four"],
      // ]
    • Get the value with the given key from this map, throwing a HashMap.MissingKeyError if the key does not exist.

      Parameters

      • key: TKey

        The key of the value to get.

      Returns TValue

      The value with the given key.

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

      map.get(2) // => "two"

      map.get(4) // => throws HashMap.MissingKeyError
    • 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.

    • Get the keys in this map as an IterableIterator.

      Returns IterableIterator<THashedKey>

      The keys in this map as an IterableIterator.

    • 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 HashMap<TKey, TValue, THashedKey>

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

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

      const swapped = map.map(([key, value]) => [value, key]);
      // => HashMap { 1 => "one", 2 => "two", 3 => "three" }
    • Create a new map with entries that have keys that are the result of calling the given callback function on each of the key-value pairs in this map.

      Parameters

      • fn: (
            key: THashedKey,
            value: TValue,
            index: number,
            original: HashMap<TKey, TValue, THashedKey>,
        ) => THashedKey

        Function to call on each key-value pair in the map. The result of this function is used as the keys for the entries in the new map.

      Returns HashMap<TKey, TValue, THashedKey>

      A new map with the entries with new keys 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.

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

      Parameters

      • fn: (
            value: TValue,
            key: THashedKey,
            index: number,
            original: HashMap<TKey, TValue, THashedKey>,
        ) => TValue

        Function to call on each key-value pair in the map. The result of this function is used as the values for the entries in the new map.

      Returns HashMap<TKey, TValue, THashedKey>

      A new map with the entries with new values 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.

    • 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.

    • Create a new map instance from an array of key-value pairs with a custom hashing function.

      Type Parameters

      • TKey
      • TValue
      • THashedKey

      Parameters

      Returns HashMap<TKey, TValue, THashedKey>

      A new map instance with the given key-value pairs and a given custom hashing function.

      const map = HashMap.fromCustomEntries(
      [
      [new Date("2020-01-01"), 1],
      [new Date("2020-02-01"), 2],
      [new Date("2020-03-01"), 3],
      ],
      {
      hash(key) {
      return key.toISOString();
      },
      });

      [...map];
      // => [
      // ["2020-01-01T00:00:00.000Z", 1],
      // ["2020-02-01T00:00:00.000Z", 2],
      // ["2020-03-01T00:00:00.000Z", 3],
      // ];
    • Create a new map instance from an array of key-value pairs using the identity as the hash function.

      Type Parameters

      • TKey
      • TValue

      Parameters

      • entries: [TKey, TValue][]

        Array of key-value pairs to create the map with.

      • options: Omit<HashMapOptions<TKey>, "hash"> = {}

        Options to use to create the new map instance.

      Returns HashMap<TKey, TValue, TKey>

      A new map instance with the given key-value pairs.

      This function uses the identity function as the hashing function meaning that keys will be stored without any changes which will therefore use the same behaviour as an ES6 Map.

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

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