invert

import { invert } from "jsr:@ende/collections";

function

function invert<T extends Record<PropertyKey, PropertyKey>>(record: Readonly<T>): InvertResult<T>

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.

record: Readonly<T>

The record to invert.

Returns

A new record with all keys and values inverted.

Example: Basic usage

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

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

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

Source