enum-plus
Version:
A drop-in replacement for native enum. Like native enum but much better!
113 lines (106 loc) • 3.56 kB
JavaScript
import { EnumCollectionClass, EnumExtensionClass } from "./enum-collection.js";
import { internalConfig, localizer } from "./global-config.js";
import { IS_ENUM } from "./utils.js";
/**
* - **EN:** Create an enum collection
* - **CN:** 创建一个枚举集合
*/
export const Enum = (init, options) => {
if (Array.isArray(init)) {
const initMap = getInitMapFromArray(init, options);
return new EnumCollectionClass(initMap, options);
} else {
return new EnumCollectionClass(init, options);
}
};
/*
* Get or set the global localization function.
* Use defineProperty here to prevent circular dependencies.
*/
// Enum.config = {};
Object.defineProperty(Enum, 'config', {
get: function () {
return internalConfig;
},
enumerable: true,
configurable: false
});
Object.defineProperty(Enum, 'localize', {
get: function () {
return localizer.localize;
},
set: function (localize) {
localizer.localize = localize;
},
enumerable: true,
configurable: false
});
Enum.extends = function (obj) {
if (obj !== undefined && Object.prototype.toString.call(obj) !== '[object Object]') {
throw new Error('The extension of Enum must be an object');
}
Object.defineProperties(EnumExtensionClass.prototype, Object.getOwnPropertyDescriptors(obj));
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Enum.install = (plugin, options) => {
plugin(options, Enum);
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Enum.isEnum = value => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return Boolean(value && typeof value === 'object' && value[IS_ENUM] === true);
};
Object.defineProperty(Enum, Symbol.hasInstance, {
value: function (instance) {
return Enum.isEnum(instance);
},
writable: false,
enumerable: false,
configurable: true
});
function getInitMapFromArray(init, options) {
const {
getValue = 'value',
getLabel = 'label',
getKey = 'key'
} = options ?? {};
return init.reduce((acc, item) => {
const value = typeof getValue === 'function' ? getValue(item) : item[getValue];
const label = typeof getLabel === 'function' ? getLabel(item) : item[getLabel];
let key = undefined;
if (getKey) {
key = typeof getKey === 'function' ? getKey(item) : item[getKey];
}
acc[key ?? value] = {
...item,
label: label || (key ?? '') || (value != null ? value.toString() : value),
value
};
return acc;
}, {});
}
/**
* - **EN:** A generic enum type that can be used to represent any enum collection
* - **CN:** 一个通用枚举类型,可以用来表示任何枚举集合
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
// @ts-expect-error: because T does not satisfy EnumInit<K, V>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
/**
* - **EN:** Represents an enumeration collection, which includes all the items in the enumeration and
* provides methods to access them.
* - **CN:** 表示一个枚举集合,包含了枚举中的所有项,并提供访问它们的方法。
*/
/**
* - **EN:** Enum initialization options
* - **CN:** 枚举初始化选项
*/
/**
* - **EN:** Represent the Enum plugin that enhances the functionality of the global Enum by adding
* new methods or properties.
* - **CN:** 表示增强Enum类功能的插件,通过添加新方法或属性
*
* @param options The options for the plugin | 插件的选项
* @param Enum The Enum global method | Enum全局方法
*/
//# sourceMappingURL=enum.js.map