UNPKG

enum-plus

Version:

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

163 lines 7.61 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 _EnumItemsArray_raw, _EnumItemsArray_localize, _EnumItemsArray_optionsConfigDefaults, _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.EnumValuesArray = exports.EnumItemsArray = void 0; const enum_1 = require("./enum"); const utils_1 = require("./utils"); /** * Enum items array, mostly are simple wrappers for EnumCollectionClass * * @template T Type of the initialization data of the enum collection * * @class EnumValuesArray * * @extends {EnumItemClass<T, K, V>[]} * * @export * * @implements {IEnumValues<T, K, V>} */ class EnumItemsArray extends Array { /** * Instantiate an enum items array * * @memberof EnumValuesArray * * @param {T} raw Original initialization data object * @param {...EnumItemClass<T[K], K, V>[]} items Enum item instance array */ constructor(raw, options, ...items) { super(...items); _EnumItemsArray_raw.set(this, void 0); _EnumItemsArray_localize.set(this, void 0); _EnumItemsArray_optionsConfigDefaults.set(this, { firstOption: false }); /** * **EN:** A boolean value indicates that this is an enum items array. * * **CN:** 布尔值,表示这是一个枚举项数组 */ this[_a] = true; __classPrivateFieldSet(this, _EnumItemsArray_raw, raw, "f"); __classPrivateFieldSet(this, _EnumItemsArray_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"); } label(keyOrValue) { var _b, _c; // First find by value, then find by key return (_c = ((_b = this.find((i) => i.value === keyOrValue)) !== null && _b !== void 0 ? _b : this.find((i) => i.key === keyOrValue))) === null || _c === void 0 ? void 0 : _c.label; } key(value) { var _b; return (_b = this.find((i) => i.value === value)) === null || _b === void 0 ? void 0 : _b.key; } has(keyOrValue) { return this.some((i) => i.value === keyOrValue || i.key === keyOrValue); } toSelect(config = __classPrivateFieldGet(this, _EnumItemsArray_optionsConfigDefaults, "f")) { var _b, _c, _d; const { firstOption = __classPrivateFieldGet(this, _EnumItemsArray_optionsConfigDefaults, "f").firstOption } = config; if (firstOption) { if (firstOption === true) { // 默认选项 const value = (_b = ('firstOptionValue' in config ? config.firstOptionValue : undefined)) !== null && _b !== void 0 ? _b : ''; const label = (_c = ('firstOptionLabel' in config ? config.firstOptionLabel : undefined)) !== null && _c !== void 0 ? _c : 'enum-plus.options.all'; return [{ key: '', value, label: __classPrivateFieldGet(this, _EnumItemsArray_localize, "f").call(this, label) }, ...this]; } else { return [ Object.assign(Object.assign({}, firstOption), { key: (_d = firstOption.key) !== null && _d !== void 0 ? _d : firstOption.value, label: __classPrivateFieldGet(this, _EnumItemsArray_localize, "f").call(this, firstOption.label) }), ...this, ]; } } else { return this; } } /** @deprecated Use `toSelect` instead */ options(config) { return this.toSelect(config); } toValueMap() { const itemsMap = {}; for (let i = 0; i < this.length; i++) { const { value, label } = this[i]; itemsMap[value] = { text: label }; } return itemsMap; } /** @deprecated Use `toValueMap` instead */ valuesEnum() { return this.toValueMap(); } toMenu() { return this.map(({ value, label }) => ({ key: value, label })); } /** @deprecated Use `toMenu` instead */ menus() { return this.toMenu(); } toFilter() { return this.map(({ value, label }) => ({ text: label, value })); } /** @deprecated Use `toFilter` instead */ filters() { return this.toFilter(); } raw(value) { if (value == null) { // Return the original initialization object return __classPrivateFieldGet(this, _EnumItemsArray_raw, "f"); } else { if (Object.keys(__classPrivateFieldGet(this, _EnumItemsArray_raw, "f")).some((k) => k === value)) { // Find by key return __classPrivateFieldGet(this, _EnumItemsArray_raw, "f")[value]; } // Find by value const itemByValue = this.find((i) => i.value === value); if (itemByValue) { return itemByValue.raw; } else { return undefined; } } } /** @deprecated Stub method, only for typing usages, not for runtime calling */ get valueType() { throw new Error('The valueType property of the enumeration is only allowed to be used to declare the ts type, and cannot be accessed at runtime! Please use the `typeof` operator in the ts type, for example: typeof Week.valueType'); } /** @deprecated Stub method, only for typing usages, not for runtime calling */ get keyType() { throw new Error('The keyType property of the enumeration is only allowed to be used to declare the ts type, and cannot be accessed at runtime! Please use the `typeof` operator in the ts type, for example: typeof Week.keyType'); } /** @deprecated Stub method, only for typing usages, not for runtime calling */ get rawType() { throw new Error('The rawType property of the enumeration is only allowed to be used to declare the ts type, and cannot be accessed at runtime! Please use the `typeof` operator in the ts type, for example: typeof Week.rawType'); } } exports.EnumItemsArray = EnumItemsArray; _EnumItemsArray_raw = new WeakMap(), _EnumItemsArray_localize = new WeakMap(), _EnumItemsArray_optionsConfigDefaults = new WeakMap(), _a = utils_1.ENUM_ITEMS; /** @deprecated Use `EnumItemsArray` instead */ class EnumValuesArray extends EnumItemsArray { } exports.EnumValuesArray = EnumValuesArray; //# sourceMappingURL=enum-values.js.map