Skip to content

Port of _.invert? #17

Open
Open

Description

@devinrhode2

Activity

  1. devinrhode2 commented on May 8, 2023

    @devinrhode2
    Author

    Code sample provided without warranty

    import { objectEntries, objectFromEntries } from 'ts-extras';
    
    /**
     * Creates an object composed of the inverted keys and values of `object`.
     *
     * Return type copied from https://stackoverflow.com/a/56416192
     */
    export const invertObject = <
      TObject extends Record<string, string>,
    >(
      object: TObject,
    ): {
      [P in keyof TObject as TObject[P]]: P;
    } => {
      return objectFromEntries(
        objectEntries(object).map(([key, value]) => [value, key]),
      ) as unknown as {
        [P in keyof TObject as TObject[P]]: P;
      };
    };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions