UNPKG

enum-plus

Version:

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

121 lines (114 loc) 5.69 kB
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } 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"; /** * - **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 = {}; Object.defineProperty(Enum, 'config', { get: function get() { return internalConfig; }, enumerable: true, configurable: false }); Object.defineProperty(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); }; Object.defineProperty(Enum, Symbol.hasInstance, { value: function value(instance) { return Enum.isEnum(instance); }, writable: false, enumerable: 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]; } acc[(_key = key) !== null && _key !== void 0 ? _key : value] = _objectSpread(_objectSpread({}, item), {}, { label: label || ((_key2 = key) !== null && _key2 !== void 0 ? _key2 : '') || (value != null ? value.toString() : value), 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