std/collections/map_keys.ts

48 lines
1.2 KiB
TypeScript
Raw Normal View History

// Copyright 2018-2024 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 keys in the given record's entries and
* returns a new record containing the transformed entries.
*
2022-11-25 11:40:23 +00:00
* If the transformed entries contain the same key multiple times, only the last
* one will appear in the returned record.
*
* @typeParam T The type of the values in the input record.
*
* @param record The record to map keys from.
* @param transformer The function to transform each key.
*
* @returns A new record with all keys transformed by the given transformer.
*
* @example Basic usage
* ```ts
* import { mapKeys } from "@std/collections/map-keys";
* import { assertEquals } from "@std/assert";
*
2022-11-25 11:40:23 +00:00
* const counts = { a: 5, b: 3, c: 8 };
*
2022-11-25 11:40:23 +00:00
* assertEquals(
* mapKeys(counts, (key) => key.toUpperCase()),
2022-11-25 11:40:23 +00:00
* {
* A: 5,
* B: 3,
* C: 8,
2022-11-25 11:40:23 +00:00
* },
* );
* ```
*/
export function mapKeys<T>(
record: Readonly<Record<string, T>>,
transformer: (key: string) => string,
): Record<string, T> {
const result: Record<string, T> = {};
for (const [key, value] of Object.entries(record)) {
const mappedKey = transformer(key);
result[mappedKey] = value;
}
return result;
}