Function invert

  • Composes a new record with all keys and values inverted.

    If the record contains duplicate values, subsequent values overwrite property assignments of previous values. If the record contains values which aren't PropertyKeys their string representation is used as the key.

    Type Parameters

    • T extends Record<PropertyKey, PropertyKey>

      The type of the input record.

    Parameters

    • record: Readonly<T>

      The record to invert.

    Returns InvertResult<T>

    A new record with all keys and values inverted.

    import { invert } from "@std/collections/invert";
    import { assertEquals } from "@std/assert";

    const record = { a: "x", b: "y", c: "z" };

    assertEquals(invert(record), { x: "a", y: "b", z: "c" });