enum-plus
Version:
A drop-in replacement for native enum. Like native enum but much better!
176 lines (166 loc) • 4.94 kB
JavaScript
import { Enum } from "./enum";
import { ENUM_ITEMS } from "./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>}
*/
export class EnumItemsArray extends Array {
#raw;
#localize;
#optionsConfigDefaults = {
firstOption: false
};
/**
* 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);
this.#raw = raw;
this.#localize = content => {
const localize = options?.localize ?? Enum.localize;
if (typeof localize === 'function') {
return localize(content);
}
return content;
};
}
/**
* **EN:** A boolean value indicates that this is an enum items array.
*
* **CN:** 布尔值,表示这是一个枚举项数组
*/
[ENUM_ITEMS] = true;
label(keyOrValue) {
// First find by value, then find by key
return (this.find(i => i.value === keyOrValue) ?? this.find(i => i.key === keyOrValue))?.label;
}
key(value) {
return this.find(i => i.value === value)?.key;
}
has(keyOrValue) {
return this.some(i => i.value === keyOrValue || i.key === keyOrValue);
}
toSelect(config = this.#optionsConfigDefaults) {
const {
firstOption = this.#optionsConfigDefaults.firstOption
} = config;
if (firstOption) {
if (firstOption === true) {
// 默认选项
const value = ('firstOptionValue' in config ? config.firstOptionValue : undefined) ?? '';
const label = ('firstOptionLabel' in config ? config.firstOptionLabel : undefined) ?? 'enum-plus.options.all';
return [{
key: '',
value,
label: this.#localize(label)
}, ...this];
} else {
return [{
...firstOption,
key: firstOption.key ?? firstOption.value,
label: this.#localize(firstOption.label)
}, ...this];
}
} else {
return this;
}
}
/** @deprecated Use `toSelect` instead */
/** @deprecated Use `toSelect` instead */
/** @deprecated Use `toSelect` instead */
/** @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();
}
// eslint-disable-next-line @typescript-eslint/ban-types
raw(value) {
if (value == null) {
// Return the original initialization object
return this.#raw;
} else {
if (Object.keys(this.#raw).some(k => k === value)) {
// Find by key
return this.#raw[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');
}
}
/** @deprecated Use `EnumItemsArray` instead */
export class EnumValuesArray extends EnumItemsArray {}
//# sourceMappingURL=enum-values.js.map