All files / enum get-enum-entries.ts

100% Statements 5/5
100% Branches 2/2
100% Functions 1/1
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23                          26x 26x 81x   51x     26x    
import type { EnumObject } from './enum-object';
 
/**
 * Lists the members of an enum as `[name, value]` pairs, without the reverse mapping of numeric enums.
 *
 * @template E - The type of the enum object.
 * @param enumObject - The enum to read, such as `Direction`.
 * @returns The `[name, value]` pairs, in declaration order.
 * @example
 * enum Direction { Up, Down }
 * getEnumEntries(Direction); // [['Up', 0], ['Down', 1]]
 */
export function getEnumEntries<E extends EnumObject>(enumObject: E): [key: keyof E & string, value: E[keyof E]][] {
  const entries: [keyof E & string, E[keyof E]][] = [];
  for (const [key, value] of Object.entries(enumObject)) {
    if (String(Number(key)) !== key) {
      // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- A value of `E`, read from `E`.
      entries.push([key, value as E[keyof E]]);
    }
  }
  return entries;
}