UNPKG

@xmobitea/gn-typescript-client

Version:

GearN Typescript Client SDK by XmobiTea (Pro)

34 lines (33 loc) 1.07 kB
export class EnumUtility { static getEnumKeys(enumType) { let keys = this.enumKeyCache.get(enumType); if (!keys) { keys = Object.keys(enumType).filter(key => isNaN(Number(key))); this.enumKeyCache.set(enumType, keys); } return keys; } static parse(enumType, value, ignoreCase) { if (!ignoreCase) { if (value in enumType) { return enumType[value]; } } else { let normalizedValue = value.toUpperCase(); let enumKeys = this.getEnumKeys(enumType); for (const key of enumKeys) { if (key.toUpperCase() === normalizedValue) { return enumType[key]; } } } return null; } static toString(enumType, value) { let enumKeys = this.getEnumKeys(enumType); const key = enumKeys.find(k => enumType[k] === value); return key !== null && key !== void 0 ? key : null; } } EnumUtility.enumKeyCache = new Map();