UNPKG

enum-plus

Version:

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

127 lines 5.54 kB
"use strict"; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _EnumItemClass_localize, _EnumItemClass_localizedProxy, _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.EnumItemClass = void 0; const enum_1 = require("./enum"); const utils_1 = require("./utils"); /** * Enum item class * * @template V General type of value * @template K General type of key * @template T Initialize object of enum item */ class EnumItemClass { /** * 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) { /** * **EN:** A boolean value indicates that this is an enum item instance. * * **CN:** 布尔值,表示这是一个枚举项实例 */ this[_a] = true; _EnumItemClass_localize.set(this, void 0); _EnumItemClass_localizedProxy.set(this, 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; }, })); this.key = key; this.value = value; this.label = label; this.raw = raw; __classPrivateFieldSet(this, _EnumItemClass_localize, (content) => { var _b; const localize = (_b = options === null || options === void 0 ? void 0 : options.localize) !== null && _b !== void 0 ? _b : enum_1.Enum.localize; if (typeof localize === 'function') { return localize(content); } return content; }, "f"); // @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 __classPrivateFieldGet(this, _EnumItemClass_localizedProxy, "f"); } toString() { var _b; return (_b = __classPrivateFieldGet(this, _EnumItemClass_localize, "f").call(this, this.label)) !== null && _b !== void 0 ? _b : this.label; } toLocaleString() { return this.toString(); } valueOf() { return this.value; } } exports.EnumItemClass = EnumItemClass; _EnumItemClass_localize = new WeakMap(), _EnumItemClass_localizedProxy = new WeakMap(), _a = utils_1.ENUM_ITEM; //# sourceMappingURL=enum-item.js.map