UNPKG

enum-plus

Version:

A drop-in replacement for native enum. Like native enum but much better!

120 lines (114 loc) 3.31 kB
import { Enum } from "./enum"; import { ENUM_ITEM } from "./utils"; /** * Enum item class * * @template V General type of value * @template K General type of key * @template T Initialize object of enum item */ export class EnumItemClass { /** Enum item value */ value; /** Enum item label (or called display name) */ label; /** Enum item key */ key; /** Original initialization object */ raw; /** * **EN:** A boolean value indicates that this is an enum item instance. * * **CN:** 布尔值,表示这是一个枚举项实例 */ [ENUM_ITEM] = true; #localize; #localizedProxy = new Proxy(this, { get: (target, prop) => { const origin = target[prop]; if (prop === 'label') { return target.toString(); } else if (typeof origin === 'function') { return origin.bind(target); } return origin; }, // Not allowed to edit set: (_, prop) => { /* istanbul ignore if */ if (!process.env.JEST_WORKER_ID) { console.warn(`Cannot modify property "${String(prop)}" on EnumItem. EnumItem instances are readonly and should not be mutated.`); } return true; }, defineProperty: (_, prop) => { /* istanbul ignore if */ if (!process.env.JEST_WORKER_ID) { console.warn(`Cannot modify property "${String(prop)}" on EnumItem. EnumItem instances are readonly and should not be mutated.`); } return true; }, deleteProperty: (_, prop) => { /* istanbul ignore if */ if (!process.env.JEST_WORKER_ID) { console.warn(`Cannot modify property "${String(prop)}" on EnumItem. EnumItem instances are readonly and should not be mutated.`); } return true; }, setPrototypeOf: () => { /* istanbul ignore if */ if (!process.env.JEST_WORKER_ID) { console.warn('Cannot change prototype of EnumItem. EnumItem instances are immutable.'); } return true; } }); /** * Instantiate an enum item * * @param key Enum item key * @param value Enum item value * @param label Enum item display name * @param raw Original initialization object * @param options Construction options */ constructor(key, value, label, raw, options) { this.key = key; this.value = value; this.label = label; this.raw = raw; this.#localize = content => { const localize = options?.localize ?? Enum.localize; if (typeof localize === 'function') { return localize(content); } return content; }; // @ts-expect-error: because override Object.toPrimitive method to return enum value this[Symbol.toPrimitive] = hint => { if (hint === 'number') { // for cases like Number(value) or +value return this.valueOf(); } else if (hint === 'string') { // for cases like String(value), `${value}` return this.toString(); } // for cases like '' + value, value == 1 return this.valueOf(); }; // Object.freeze(this); } readonly() { return this.#localizedProxy; } toString() { return this.#localize(this.label) ?? this.label; } toLocaleString() { return this.toString(); } valueOf() { return this.value; } } //# sourceMappingURL=enum-item.js.map