enum-plus
Version:
A drop-in replacement for native enum. Like native enum but much better!
189 lines • 7.17 kB
JavaScript
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EnumCollectionClass = exports.EnumExtensionClass = void 0;
const enum_item_1 = require("./enum-item");
const enum_values_1 = require("./enum-values");
const localize_1 = require("./localize");
const utils_1 = require("./utils");
/**
* **EN:** Enum collection extension base class, used to extend the Enums
*
* **CN:** 枚举集合扩展基类,用于扩展枚举
*/
// @ts-expect-error: because of typing extend in tests
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
class EnumExtensionClass {
}
exports.EnumExtensionClass = EnumExtensionClass;
/**
* **EN:** Enum collection
*
* **CN:** 枚举项集合
*/
class EnumCollectionClass extends EnumExtensionClass {
/**
* 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() {
var _b, _c, _d, _e;
const localize = (_c = (_b = this._options) === null || _b === void 0 ? void 0 : _b.localize) !== null && _c !== void 0 ? _c : localize_1.localizer.localize;
if (typeof localize === 'function') {
return localize((_d = this._options) === null || _d === void 0 ? void 0 : _d.name);
}
return (_e = this._options) === null || _e === void 0 ? void 0 : _e.name;
}
constructor(init = {}, options) {
super();
/**
* **EN:** A boolean value indicates that this is an enum collection instance.
*
* **CN:** 布尔值,表示这是一个枚举集合实例
*/
this[_a] = true;
this._options = options;
// exclude number keys with a "reverse mapping" value, it means those "reverse mapping" keys of number enums
const keys = Object.keys(init).filter((k) => { var _b; return !(/^-?\d+$/.test(k) && k === `${(_b = init[init[k]]) !== null && _b !== void 0 ? _b : ''}`); });
const parsed = keys.map((key) => parseEnumItem(init[key], key));
keys.forEach((key, index) => {
const { value } = parsed[index];
// @ts-expect-error: because of dynamically define property
this[key] = value;
});
Object.freeze(keys);
// @ts-expect-error: because use KEYS to avoid naming conflicts in case of 'keys' field name is taken
this[Object.keys(init).some((k) => k === 'keys') ? utils_1.KEYS : 'keys'] = keys;
// Build enum item data
const items = new enum_values_1.EnumItemsArray(init, this._options, ...keys.map((key, index) => {
const { value, label } = parsed[index];
return new enum_item_1.EnumItemClass(key, value, label, init[key], this._options).readonly();
}));
// @ts-expect-error: because use ITEMS to avoid naming conflicts in case of 'items' field name is taken
this[Object.keys(init).some((k) => k === 'items') ? utils_1.ITEMS : 'items'] = items;
// @ts-expect-error: because use VALUES to avoid naming conflicts in case of 'values' field name is taken
this[Object.keys(init).some((k) => k === 'values') ? utils_1.VALUES : 'values'] = items;
// Override the `instanceof` operator rule
// @ts-expect-error: because override the instanceof operator
this[Symbol.hasInstance] = (instance) => {
// intentionally use == to support both number and string format value
return this.items.some(
// eslint-disable-next-line eqeqeq
(i) => instance == i.value || instance === i.key);
};
Object.freeze(this);
Object.freeze(this.items);
Object.freeze(this.keys);
}
label(keyOrValue) {
return this.items.label(keyOrValue);
}
key(value) {
return this.items.key(value);
}
raw(value) {
if (value != null) {
return this.items.raw(value);
}
else {
return this.items.raw();
}
}
has(keyOrValue) {
return this.items.has(keyOrValue);
}
toSelect(config) {
return this.items.toSelect(config);
}
options(config) {
return this.items.options(config);
}
toMenu() {
return this.items.toMenu();
}
/** @deprecated use `toMenu` instead */
menus() {
return this.items.menus();
}
toFilter() {
return this.items.toFilter();
}
/** @deprecated use `toFilter` instead */
filters() {
return this.items.filters();
}
toValueMap() {
return this.items.toValueMap();
}
/** @deprecated use `toValueMap` instead */
valuesEnum() {
return this.items.valuesEnum();
}
get valueType() {
return this.items.valueType;
}
get keyType() {
return this.items.keyType;
}
get rawType() {
return this.items.rawType;
}
}
exports.EnumCollectionClass = EnumCollectionClass;
_a = utils_1.ENUM_COLLECTION;
function parseEnumItem(init, key) {
var _b, _c;
let value;
let label;
if (init != null) {
if (typeof init === 'number' || typeof init === 'string' || typeof init === 'symbol') {
value = init;
label = key;
}
else if (typeof init === 'object') {
// Initialize using object
if (Object.prototype.toString.call(init) === '[object Object]') {
if ('value' in init && Object.keys(init).some((k) => k === 'value')) {
// type of {value, label}
value = (_b = init.value) !== null && _b !== void 0 ? _b : key;
if ('label' in init && Object.keys(init).some((k) => k === 'label')) {
label = init.label;
}
else {
label = key;
}
}
else if ('label' in init && Object.keys(init).some((k) => k === 'label')) {
// typeof {label}
value = key;
label = (_c = init.label) !== null && _c !== void 0 ? _c : key;
}
else {
// {} empty object
value = key;
label = key;
}
}
else {
// Probably Date, RegExp and other primitive types
value = init;
label = key;
}
}
else {
throw new Error(`Invalid enum item: ${JSON.stringify(init)}`);
}
}
else {
value = key;
label = key;
}
return { value, label };
}
//# sourceMappingURL=enum-collection.js.map
;