UNPKG

enum-plus

Version:

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

123 lines (117 loc) 4.7 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } import { EnumCollectionClass, EnumExtensionClass } from "./enum-collection.js"; import { internalConfig, localizer } from "./global-config.js"; import { IS_ENUM } from "./utils.js"; var define = Object.defineProperty; /** * - **EN:** Create an enum collection * - **CN:** 创建一个枚举集合 */ export var Enum = function Enum(init, options) { if (Array.isArray(init)) { var 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 = {}; define(Enum, 'config', { get: function get() { return internalConfig; }, enumerable: true, configurable: false }); define(Enum, 'localize', { get: function get() { return localizer.localize; }, set: function set(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 = function (plugin, options) { plugin(options, Enum); }; // eslint-disable-next-line @typescript-eslint/no-explicit-any Enum.isEnum = function (value) { // eslint-disable-next-line @typescript-eslint/no-explicit-any return Boolean(value && _typeof(value) === 'object' && value[IS_ENUM] === true); }; define(Enum, Symbol.hasInstance, { value: function value(instance) { return Enum.isEnum(instance); }, writable: false, configurable: true }); function getInitMapFromArray(init, options) { var _ref = options !== null && options !== void 0 ? options : {}, _ref$getValue = _ref.getValue, getValue = _ref$getValue === void 0 ? 'value' : _ref$getValue, _ref$getLabel = _ref.getLabel, getLabel = _ref$getLabel === void 0 ? 'label' : _ref$getLabel, _ref$getKey = _ref.getKey, getKey = _ref$getKey === void 0 ? 'key' : _ref$getKey; return init.reduce(function (acc, item) { var _key, _key2; var value = typeof getValue === 'function' ? getValue(item) : item[getValue]; var label = typeof getLabel === 'function' ? getLabel(item) : item[getLabel]; var key = undefined; if (getKey) { key = typeof getKey === 'function' ? getKey(item) : item[getKey]; } var rawItem = Object.assign({}, item, { label: label || ((_key = key) !== null && _key !== void 0 ? _key : '') || (value != null ? value.toString() : value), value: value }); delete rawItem.key; acc[(_key2 = key) !== null && _key2 !== void 0 ? _key2 : value] = rawItem; 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:** 表示一个枚举集合,包含了枚举中的所有项,并提供访问它们的方法。 * * @template T The type of enum initialization | 枚举初始化的类型 * @template K The type of enum keys | 枚举键的类型 * @template V The type of enum values | 枚举值的类型 * @template LP The type of enum item's label prefix | 枚举项标签的前缀值 * @template OP The type of enum initialization options | 枚举初始化选项的类型 */ /** * - **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