import invariant from 'invariant'; /** * If the specified key is not already associated with a value in this map, computes * its value using the given mapping function and enters it into this map. * * @param map - map from which to get the key's value or compute and associate * @param key - key for which to get the value or with which the computed value is to be associated * @param mappingFunction - function to compute a value for key */ export const computeIfAbsent = ( map: Map, key: K, mappingFunction: (key: K) => V, ): V => { if (!map.has(key)) { const value = mappingFunction(key); map.set(key, value); } return map.get(key)!; }; /** * Create a new Map by associating the value of mapper executed for each key in the source map. * * @param map - source map * @param mapper - function to compute a value in the resulting map for the source key and value */ export const mapMap = ( map: ReadonlyMap, mapper: (value: V, key: K) => M, ): Map => { const resultingMap = new Map(); for (const [k, v] of map) { resultingMap.set(k, mapper(v, k)); } return resultingMap; }; /** * Create a new Map by associating the value of mapper executed for each key in the source map. * * @param map - source map * @param mapper - asynchronous function to compute a value in the resulting map for the source key and value */ export const mapMapAsync = async function ( map: ReadonlyMap, mapper: (value: V, key: K) => Promise, ): Promise> { const resultingMap = new Map(); await Promise.all( Array.from(map.keys()).map(async (k) => { const initialValue = map.get(k) as V; const result = await mapper(initialValue, k); resultingMap.set(k, result); }), ); return resultingMap; }; /** * Create a new Map by associating the value of each key with mapper executed for each key in the source map. * The opposite of mapMap. In the event two source keys map to the same result key, the second source key's * value will overwrite the first, in which case the cardinality of the returned map may be smaller than the * source map's. * * @param map - source map * @param mapper - function to compute a key in the resulting map for the source key and value */ export const mapKeys = ( map: ReadonlyMap, mapper: (key: K, value: V) => K2, ): Map => { const resultingMap = new Map(); for (const [k, v] of map) { resultingMap.set(mapper(k, v), v); } return resultingMap; }; /** * Create a new Map from each member of keys to the corresponding member of values. * * @example * ``` * zipToMap([1, 2], ['a', 'b']) => Map({1: 'a', 2: 'b'}) * ``` * * @param keys - keys * @param values - corresponding ordered values for keys */ export const zipToMap = (keys: readonly K[], values: readonly V[]): Map => { invariant( keys.length === values.length, `zipToMap input length mismatch: keys[${keys.length}], values[${values.length}]`, ); const resultingMap = new Map(); for (let i = 0; i < keys.length; i++) { resultingMap.set(keys[i]!, values[i]!); } return resultingMap; }; /** * Create a new Map by inverting keys and values of specified map. * * @param map - map to invert */ export const invertMap = (map: ReadonlyMap): Map => { const resultingMap = new Map(); for (const [k, v] of map) { resultingMap.set(v, k); } return resultingMap; }; /** * Execute a reducer function on each element of the source map, resulting in a single output value. * * @param map - source map * @param reducer - reducer function that takes an accumulated value, current iteration value, and current * iteration key and returns a new accumulated value * @param initialValue - initial accumulated value */ export const reduceMap = ( map: ReadonlyMap, reducer: (accumulator: A, value: V, key: K) => A, initialValue: A, ): A => { let newAccumulator = initialValue; for (const [k, v] of map) { newAccumulator = reducer(newAccumulator, v, k); } return newAccumulator; }; /** * Execute an asynchronous reducer function on each element of the source map, resulting in a single output value. * Note that this does not parallelize asynchronous reduce steps so it should be used with caution. * * @param map - source map * @param reducer - asynchronous reducer function that takes an accumulated value, current iteration value, and * current iteration key and returns a new accumulated value * @param initialValue - initial accumulated value */ export const reduceMapAsync = async ( map: ReadonlyMap, reducer: (accumulator: A, value: V, key: K) => Promise, initialValue: A, ): Promise => { let newAccumulator = initialValue; for (const [k, v] of map) { newAccumulator = await reducer(newAccumulator, v, k); } return newAccumulator; }; /** * Create a new Map containing all elements from the source map that pass the provided test predicate. * @param map - source map * @param predicate - function to test each element of source map */ export function filterMap( map: ReadonlyMap, predicate: (value: V, key: K) => value is S, ): Map; /** * Create a new Map containing all elements from the source map that pass the provided test predicate. * @param map - source map * @param predicate - function to test each element of source map */ export function filterMap( map: ReadonlyMap, predicate: (value: V, key: K) => boolean, ): Map; /** * Create a new Map containing all elements from the source map that pass the provided test predicate. * @param map - source map * @param predicate - function to test each element of source map */ export function filterMap( map: ReadonlyMap, predicate: (value: V, key: K) => unknown, ): Map { const resultingMap = new Map(); map.forEach((v, k) => { if (predicate(v, k)) { resultingMap.set(k, v); } }); return resultingMap; }