wrote helper function fto accomodate improper enums

This commit is contained in:
Carter 2024-12-13 09:39:19 -06:00
parent 438451690b
commit 23bcb467e9

View file

@ -44,18 +44,29 @@ const DEVICE_STATE_MAP = new Map<pond.DeviceState, DeviceStateHelper>([
[pond.DeviceState.DEVICE_STATE_MISSING, Missing] [pond.DeviceState.DEVICE_STATE_MISSING, Missing]
]); ]);
export function compareEnum<T>(value: string | number, enumMember: T): boolean { export function compareEnum<T extends object>(value: T[keyof T], e: T, enumMember: T[keyof T]): boolean {
// Get the enum object from the enum member // let has = false
const enumObj = Object.freeze(enumMember as any).constructor; // if (typeof value === 'string') {
// Object.values(e).forEach((val: keyof T) => {
// Check if the value matches either the key or the value of the enum member if (e[value as keyof T] === enumMember) {
return Object.keys(enumObj).some( // has = true
key => enumObj[key] === value || key === value return true
); }
return false
// })
// } else if (typeof value === 'number') {
// Object.keys(e as any).forEach(val => {
// if (val === enumMember) {
// has = true
// return
// }
// })
// }
// return has
} }
export function getDeviceStateHelper(state: pond.DeviceState): DeviceStateHelper { export function getDeviceStateHelper(state: pond.DeviceState): DeviceStateHelper {
if (compareEnum(state, pond.DeviceState.DEVICE_STATE_OK)) { if (compareEnum(state, pond.DeviceState, pond.DeviceState.DEVICE_STATE_OK)) {
return OK; return OK;
} }
const deviceStates = Array.from(DEVICE_STATE_MAP.keys()); const deviceStates = Array.from(DEVICE_STATE_MAP.keys());