enum-plus
Version:
A drop-in replacement for native enum. Like native enum but much better!
169 lines • 6.05 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 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 {
constructor(init = {}, options) {
super();
/**
* **EN:** A boolean value indicates that this is an enum collection instance.
*
* **CN:** 布尔值,表示这是一个枚举集合实例
*/
this[_a] = true;
// 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, options, ...keys.map((key, index) => {
const { value, label } = parsed[index];
return new enum_item_1.EnumItemClass(key, value, label, init[key], 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);
}
key(value) {
return this.items.key(value);
}
label(keyOrValue) {
return this.items.label(keyOrValue);
}
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();
}
raw(value) {
if (value != null) {
return this.items.raw(value);
}
else {
return this.items.raw();
}
}
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
;