std/collections/map_entries.ts

45 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-01-03 10:47:44 +00:00
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
/**
2022-11-25 11:40:23 +00:00
* Applies the given transformer to all entries in the given record and returns
* a new record containing the results.
*
2022-11-25 11:40:23 +00:00
* @example
* ```ts
* import { mapEntries } from "https://deno.land/std@$STD_VERSION/collections/map_entries.ts";
* import { assertEquals } from "https://deno.land/std@$STD_VERSION/assert/assert_equals.ts";
*
* const usersById = {
2022-11-25 11:40:23 +00:00
* "a2e": { name: "Kim", age: 22 },
* "dfe": { name: "Anna", age: 31 },
* "34b": { name: "Tim", age: 58 },
* } as const;
2022-11-25 11:40:23 +00:00
* const agesByNames = mapEntries(usersById, ([id, { name, age }]) => [name, age]);
*
2022-11-25 11:40:23 +00:00
* assertEquals(
* agesByNames,
* {
* "Kim": 22,
* "Anna": 31,
* "Tim": 58,
* },
* );
* ```
*/
export function mapEntries<T, O>(
record: Readonly<Record<string, T>>,
transformer: (entry: [string, T]) => [string, O],
): Record<string, O> {
const ret: Record<string, O> = {};
const entries = Object.entries(record);
for (const entry of entries) {
const [mappedKey, mappedValue] = transformer(entry);
ret[mappedKey] = mappedValue;
}
return ret;
}