All files / map/src mapValues.ts

100% Statements 5/5
100% Branches 0/0
100% Functions 3/3
100% Lines 3/3

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23  1x                               6x     6x    
import { Mapper } from "@benneq/function";
import { fromIterable } from "./fromIterable";
 
/**
 * Maps the values of a {@link Map} from type `S` to `T`
 *
 * @example
 * Map all values from number to string
 * ```ts
 * const mapper = mapValues((n) => String(n));
 * const map = mapValues(new Map([1, 1]));
 * console.log(map); // Map([[1, "1"]])
 * ```
 *
 * @param mapper
 * @returns the new {@link Map} with the mapped values
 */
export const mapValues = <S, T>(
  mapper: Mapper<S, T>
): (<K>(map: Map<K, S>) => Map<K, T>) => {
  return fromIterable(([k, v]) => [k, mapper(v)]);
};