Class Map

Additional static members for the native Map class.

Does not extend the native Map class.

Constructors

Methods

  • Delete the provided key from the map.

    Type Parameters

    • K
    • V = K

    Parameters

    • map: Map<K, V>

      the map to mutate

    • key: K

      the key to delete

    • comparator: ((a, b) => boolean)

      a comparator function of keys

        • (a, b): boolean
        • Parameters

          Returns boolean

    Returns boolean

    Was the map mutated?

  • Return a new map with entries that pass the provided predicate function.

    Type Parameters

    • K
    • V

    Parameters

    • map: ReadonlyMap<K, V>

      the map to filter

    • predicate: ((value, key, map) => boolean)

      function to test each entry of the map

        • (value, key, map): boolean
        • Parameters

          • value: V
          • key: K
          • map: ReadonlyMap<K, V>

          Returns boolean

    • this_arg: unknown = null

      object to use as this when executing predicate

    Returns Map<K, V>

    a new map with the entries that pass the test; if no entries pass, an empty map is returned

    See

    https://github.com/tc39/proposal-collection-methods

    Typeparam

    • K the type of keys in the map

    Typeparam

    • V the type of values in the map
  • Return a value found in the map that satisfies the predicate, or null if none is found.

    Type Parameters

    • K
    • V

    Parameters

    • map: ReadonlyMap<K, V>

      the map to search

    • predicate: ((value, key, map) => boolean)

      function to test each entry of the map

        • (value, key, map): boolean
        • Parameters

          • value: V
          • key: K
          • map: ReadonlyMap<K, V>

          Returns boolean

    • this_arg: unknown = null

      object to use as this when executing predicate

    Returns null | V

    the value found, or null if none is found

    See

    https://github.com/tc39/proposal-collection-methods

    Typeparam

    • K the type of keys in the map

    Typeparam

    • V the type of values in the map
  • Return a key found in the map that satisfies the predicate, or null if none is found.

    Type Parameters

    • K
    • V

    Parameters

    • map: ReadonlyMap<K, V>

      the map to search

    • predicate: ((key, index, map) => boolean)

      function to test each key of the map

        • (key, index, map): boolean
        • Parameters

          • key: K
          • index: number
          • map: ReadonlyMap<K, V>

          Returns boolean

    • this_arg: unknown = null

      object to use as this when executing predicate

    Returns null | K

    the key found, or null if none is found

    See

    https://github.com/tc39/proposal-collection-methods

    Typeparam

    • K the type of keys in the map

    Typeparam

    • V the type of values in the map
  • Perform a callback function on a map, aggregating any errors caught.

    Instead of throwing on the first error and stopping iteration, as Map#forEach would do, this method continues performing the callback on the rest of the map until all items are done. If only one error was caught, then that error is simply rethrown. However, if more errors were caught, they are collected into a single AggregateError, which is then thrown. If no errors are caught, this method returns void.

    Type Parameters

    • K
    • V

    Parameters

    • map: ReadonlyMap<K, V>

      the map of keys and values

    • callback: ((value, key, src) => void)

      the function to call on each key–value pair

        • (value, key, src): void
        • Parameters

          • value: V
          • key: K
          • src: ReadonlyMap<K, V>

          Returns void

    Returns void

    Example

    xjs.Map.forEachAggregated<number>(new Map<string, number>([
    ['one', 1],
    ['two', 2],
    ['three', 3],
    ['four', 4],
    ]), (num, name) => {
    if (num % 2 === 0) {
    throw new Error(`${ name } is even.`);
    };
    });
    // Expected thrown error:
    AggregateError {
    errors: [
    Error { message: "two is even." },
    Error { message: "four is even." },
    ]
    }

    Typeparam

    K the type of keys in the map

    Typeparam

    V the type of values in the map

    Throws

    if two or more iterations throws an error

    Throws

    if one iteration throws an error

  • Get the value of the provided key from the map.

    Type Parameters

    • K
    • V = K

    Parameters

    • map: ReadonlyMap<K, V>

      the map to check

    • key: K

      the key to check

    • comparator: ((a, b) => boolean)

      a comparator function of keys

        • (a, b): boolean
        • Parameters

          Returns boolean

    Returns undefined | V

    the value corresponding to the key

  • Return whether the provided key exists in the map.

    Type Parameters

    • K
    • V = K

    Parameters

    • map: ReadonlyMap<K, V>

      the map to check

    • key: K

      the key to check

    • comparator: ((a, b) => boolean)

      a comparator function of keys

        • (a, b): boolean
        • Parameters

          Returns boolean

    Returns boolean

    Does the set have the given key?

  • Test whether two maps have “the same” key–value pairs.

    Similar to xjs_Set.is, where the order is not important, but also has the option to check equality of keys.

    Type Parameters

    • K
    • V

    Parameters

    • a: ReadonlyMap<K, V>

      the first map

    • b: ReadonlyMap<K, V>

      the second map

    • predicates: {
          keys?: ((x, y) => boolean);
          values?: ((x, y) => boolean);
      } = {}

      for checking equality for keys/values

      • Optional keys?: ((x, y) => boolean)

        check the “sameness” of corresponding keys of a and b

          • (x, y): boolean
          • Parameters

            Returns boolean

      • Optional values?: ((x, y) => boolean)

        check the “sameness” of corresponding values of a and b

          • (x, y): boolean
          • Parameters

            Returns boolean

    Returns boolean

    Are corresponding pairs the same, i.e. replaceable?

    Typeparam

    K - the type of keys in the maps

    Typeparam

    V - the type of values in the maps

  • Return a new Map with the results of calling a provided function on every key in the given Map.

    Type Parameters

    • K
    • V
    • T

    Parameters

    • map: ReadonlyMap<K, V>

      the map to map

    • callback: ((value, key, map) => T)

      the function to call on each key of the map

        • (value, key, map): T
        • Parameters

          • value: V
          • key: K
          • map: ReadonlyMap<K, V>

          Returns T

    • this_arg: unknown = null

      object to use as this when executing callback

    Returns Map<T, V>

    a new Map with transformed keys obtained from callback and the same values

    See

    https://github.com/tc39/proposal-collection-methods

    Typeparam

    • K the type of keys in the map

    Typeparam

    • V the type of values in the map

    Typeparam

    • T the type of new keys returned by the callback
  • Return a new Map with the results of calling a provided function on every value in the given Map.

    Type Parameters

    • K
    • V
    • T

    Parameters

    • map: ReadonlyMap<K, V>

      the map to map

    • callback: ((value, key, map) => T)

      the function to call on each value of the map

        • (value, key, map): T
        • Parameters

          • value: V
          • key: K
          • map: ReadonlyMap<K, V>

          Returns T

    • this_arg: unknown = null

      object to use as this when executing callback

    Returns Map<K, T>

    a new Map with the same keys and transformed values obtained from callback

    See

    https://github.com/tc39/proposal-collection-methods

    Typeparam

    • K the type of keys in the map

    Typeparam

    • V the type of values in the map

    Typeparam

    • T the type of new values returned by the callback
  • Set a value to the provided key in the map.

    Type Parameters

    • K
    • V = K

    Parameters

    • map: Map<K, V>

      the map to mutate

    • key: K

      the key to check

    • value: V

      the value to set

    • comparator: ((a, b) => boolean)

      a comparator function of keys

        • (a, b): boolean
        • Parameters

          Returns boolean

    Returns Map<K, V>

    the mutated map

  • Set, then return, a new value.

    Type Parameters

    • K
    • V

    Parameters

    • map: Map<K, V>

      the map to set

    • key: K

      the key to check

    • value: V

      the value to set

    • Optional comparator: ((a, b) => boolean)

      a comparator function of keys

        • (a, b): boolean
        • Parameters

          Returns boolean

    Returns V

    the value