Function mapKeys

  • Applies the given transformer to all keys in the given record's entries and returns a new record containing the transformed entries.

    If the transformed entries contain the same key multiple times, only the last one will appear in the returned record.

    Type Parameters

    • T

      The type of the values in the input record.

    Parameters

    • record: Readonly<Record<string, T>>

      The record to map keys from.

    • transformer: ((key: string) => string)

      The function to transform each key.

        • (key): string
        • Parameters

          • key: string

          Returns string

    Returns Record<string, T>

    A new record with all keys transformed by the given transformer.

    import { mapKeys } from "@std/collections/map-keys";
    import { assertEquals } from "@std/assert";

    const counts = { a: 5, b: 3, c: 8 };

    assertEquals(
    mapKeys(counts, (key) => key.toUpperCase()),
    {
    A: 5,
    B: 3,
    C: 8,
    },
    );