ENUMS: Initial Enum Helper rework + Reorganization (#596)

This commit is contained in:
Snarling
2023-06-12 00:34:20 -04:00
committed by GitHub
parent 6ed8ea9796
commit 6732549196
224 changed files with 2126 additions and 2171 deletions
-13
View File
@@ -1,19 +1,6 @@
import { getRandomInt } from "./getRandomInt";
/** Verifies that a supplied value is a member of the provided object/enum. Works for enums as well as enum-like objects (const {} as const). */
export function checkEnum<T extends object>(obj: T, value: unknown): value is T[keyof T] {
return Object.values(obj).includes(value);
}
export function findEnumMember<T extends Record<string, string>>(obj: T, value: string): T[keyof T] | undefined {
const lowerValue = value.toLowerCase().replace(/ /g, "");
for (const member of Object.values(obj) as T[keyof T][]) {
if (lowerValue.includes(member.toLowerCase().replace(/ /g, ""))) return member;
}
}
export function getRandomMember<T extends Record<string, string>>(obj: T): T[keyof T] {
const array = Object.values(obj);
const index = getRandomInt(0, array.length - 1);
return array[index] as T[keyof T];
}