UNPKG

enum-plus

Version:

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

146 lines (137 loc) 4.8 kB
import { EnumItemsArray } from "./enum-items.js"; import { localizer } from "./global-config.js"; import { ENUM_OPTIONS, IS_ENUM, ITEMS, KEYS, LABELS, META, NAMED, VALUES } from "./utils.js"; /** * - **EN:** Enum collection extension base class, used to extend the Enums * - **CN:** 枚举集合扩展基类,用于扩展枚举 */ // @ts-expect-error: because don't know which methods are added // eslint-disable-next-line @typescript-eslint/no-extraneous-class export class EnumExtensionClass {} /** * - **EN:** Enum collection * - **CN:** 枚举项集合 */ export class EnumCollectionClass extends EnumExtensionClass { __options__; // used for e2e serialization _ds; constructor(init = {}, options) { super(); const define = Object.defineProperty; const freeze = Object.freeze; // Do not use class field here, because don't want print this field in Node.js define(this, '__options__', { value: options }); const keys = Object.keys(init); // Generate enum items array const items = new EnumItemsArray(init, options); freeze(items); // @ts-expect-error: because use ITEMS to avoid naming conflicts in case of 'items' field name is taken this[keys.includes('items') ? ITEMS : 'items'] = items; define(this, '_ds', { value: items }); // @ts-expect-error: because use KEYS to avoid naming conflicts in case of 'keys' field name is taken this[keys.includes('keys') ? KEYS : 'keys'] = items[KEYS]; // @ts-expect-error: because use VALUES to avoid naming conflicts in case of 'values' field name is taken this[keys.includes('values') ? VALUES : 'values'] = items[VALUES]; // @ts-expect-error: because use NAMED to avoid naming conflicts in case of 'named' field name is taken this[keys.includes('named') ? NAMED : 'named'] = items.named; // @ts-expect-error: because use META to avoid naming conflicts in case of 'meta' field name is taken this[keys.includes('meta') ? META : 'meta'] = items.meta; // Add keys to the instance, allows picking enum values by keys items.forEach(item => { // @ts-expect-error: because of dynamic property this[item.key] = item.value; }); // @ts-expect-error: because use LABELS to avoid naming conflicts in case of 'labels' field name is taken define(this, keys.includes('labels') ? LABELS : 'labels', { get: function () { return this._ds.labels; }, enumerable: true }); freeze(this); } /** * - **EN:** A boolean value indicates that this is an enum collection instance. * - **CN:** 布尔值,表示这是一个枚举集合实例 */ // Do not use readonly field here, because don't want print this field in Node.js // eslint-disable-next-line @typescript-eslint/class-literal-property-style get [IS_ENUM]() { return true; } /** * - **EN:** Get the options to initialize the enum. * - **CN:** 获取初始化枚举时的选项 */ get [ENUM_OPTIONS]() { return this.__options__; } [Symbol.hasInstance](instance) { return instance instanceof this._ds; } /** * The enum collection name, supports localization. Note that it usually returns a string, but if * a custom `localize` function is set, the return value may vary depending on the implementation * of the method. * * - **CN:** 枚举集合显示名称,支持本地化。注意,通常情况下返回的是字符串,但如果设置了自定义的 `localize` 函数,则返回值可能有所不同,取决于方法的实现 * * @returns {string | undefined} The localized name of the enum collection, or undefined if not * set. */ get name() { const opts = this.__options__; if (typeof opts?.name === 'function') { return opts.name(undefined); } const localize = opts?.localize ?? localizer.localize; if (typeof localize === 'function') { return localize(opts?.name); } return opts?.name; } label(keyOrValue) { return this._ds.label(keyOrValue); } key(value) { return this._ds.key(value); } item(keyOrValue) { return this._ds.item(keyOrValue); } raw(value) { if (value != null) { return this._ds.raw(value); } else { return this._ds.raw(); } } has(keyOrValue) { return this._ds.has(keyOrValue); } // eslint-disable-next-line @typescript-eslint/no-explicit-any findBy(...rest) { return this._ds.findBy(...rest); } toList(config) { return this._ds.toList(config); } toMap(config) { return this._ds.toMap(config); } get valueType() { return this._ds.valueType; } get keyType() { return this._ds.keyType; } get rawType() { return this._ds.rawType; } } //# sourceMappingURL=enum-collection.js.map