UNPKG

enum-plus

Version:

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

202 lines (193 loc) 10.8 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); } var _Symbol$hasInstance; function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } 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 _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } 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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } import { EnumItemsArray } from "./enum-items.js"; import { localizer } from "./global-config.js"; import { ENUM_OPTIONS, IS_ENUM, ITEMS, KEYS, LABELS, META, NAMED, VALUES } from "./utils.js"; /** * - **EN:** Enum collection extension base class, used to extend the Enums * - **CN:** 枚举集合扩展基类,用于扩展枚举 */ // @ts-expect-error: because don't know which methods are added // eslint-disable-next-line @typescript-eslint/no-extraneous-class export var EnumExtensionClass = /*#__PURE__*/_createClass(function EnumExtensionClass() { _classCallCheck(this, EnumExtensionClass); }); /** * - **EN:** Enum collection * - **CN:** 枚举项集合 */ _Symbol$hasInstance = Symbol.hasInstance; export var EnumCollectionClass = /*#__PURE__*/function (_ref) { _inherits(EnumCollectionClass, _ref); var _super = _createSuper(EnumCollectionClass); function EnumCollectionClass() { var _this; var init = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var options = arguments.length > 1 ? arguments[1] : undefined; _classCallCheck(this, EnumCollectionClass); _this = _super.call(this); // Do not use class field here, because don't want print this field in Node.js _defineProperty(_assertThisInitialized(_this), "__options__", void 0); // used for e2e serialization _defineProperty(_assertThisInitialized(_this), "__items__", void 0); Object.defineProperty(_assertThisInitialized(_this), '__options__', { value: options, writable: false, enumerable: false, configurable: false }); var keys = Object.keys(init); // Generate enum items array var items = new EnumItemsArray(init, options); Object.freeze(items); // @ts-expect-error: because use ITEMS to avoid naming conflicts in case of 'items' field name is taken _this[keys.includes('items') ? ITEMS : 'items'] = items; Object.defineProperty(_assertThisInitialized(_this), '__items__', { value: items, writable: false, enumerable: false, configurable: false }); // @ts-expect-error: because use KEYS to avoid naming conflicts in case of 'keys' field name is taken _this[keys.includes('keys') ? KEYS : 'keys'] = items[KEYS]; // @ts-expect-error: because use VALUES to avoid naming conflicts in case of 'values' field name is taken _this[keys.includes('values') ? VALUES : 'values'] = items[VALUES]; // @ts-expect-error: because use NAMED to avoid naming conflicts in case of 'named' field name is taken _this[keys.includes('named') ? NAMED : 'named'] = items.named; // @ts-expect-error: because use META to avoid naming conflicts in case of 'meta' field name is taken _this[keys.includes('meta') ? META : 'meta'] = items.meta; // Add keys to the instance, allows picking enum values by keys items.forEach(function (item) { // @ts-expect-error: because of dynamic property _this[item.key] = item.value; }); // @ts-expect-error: because use LABELS to avoid naming conflicts in case of 'labels' field name is taken Object.defineProperty(_assertThisInitialized(_this), keys.includes('labels') ? LABELS : 'labels', { enumerable: true, configurable: false, get: function get() { return this.__items__.labels; } }); Object.freeze(_assertThisInitialized(_this)); return _this; } /** * - **EN:** A boolean value indicates that this is an enum collection instance. * - **CN:** 布尔值,表示这是一个枚举集合实例 */ // Do not use readonly field here, because don't want print this field in Node.js // eslint-disable-next-line @typescript-eslint/class-literal-property-style _createClass(EnumCollectionClass, [{ key: IS_ENUM, get: function get() { return true; } /** * - **EN:** Get the options to initialize the enum. * - **CN:** 获取初始化枚举时的选项 */ }, { key: ENUM_OPTIONS, get: function get() { return this.__options__; } }, { key: _Symbol$hasInstance, value: function value(instance) { return instance instanceof this.__items__; } /** * The enum collection name, supports localization. Note that it usually returns a string, but if * a custom `localize` function is set, the return value may vary depending on the implementation * of the method. * * - **CN:** 枚举集合显示名称,支持本地化。注意,通常情况下返回的是字符串,但如果设置了自定义的 `localize` 函数,则返回值可能有所不同,取决于方法的实现 * * @returns {string | undefined} The localized name of the enum collection, or undefined if not * set. */ }, { key: "name", get: function get() { var _this$__options__$loc, _this$__options__, _this$__options__3; var localize = (_this$__options__$loc = (_this$__options__ = this.__options__) === null || _this$__options__ === void 0 ? void 0 : _this$__options__.localize) !== null && _this$__options__$loc !== void 0 ? _this$__options__$loc : localizer.localize; if (typeof localize === 'function') { var _this$__options__2; return localize((_this$__options__2 = this.__options__) === null || _this$__options__2 === void 0 ? void 0 : _this$__options__2.name); } return (_this$__options__3 = this.__options__) === null || _this$__options__3 === void 0 ? void 0 : _this$__options__3.name; } }, { key: "label", value: function label(keyOrValue) { return this.__items__.label(keyOrValue); } }, { key: "key", value: function key(value) { return this.__items__.key(value); } }, { key: "raw", value: function raw(value) { if (value != null) { return this.__items__.raw(value); } else { return this.__items__.raw(); } } }, { key: "has", value: function has(keyOrValue) { return this.__items__.has(keyOrValue); } // eslint-disable-next-line @typescript-eslint/no-explicit-any }, { key: "findBy", value: function findBy() { var _this$__items__; return (_this$__items__ = this.__items__).findBy.apply(_this$__items__, arguments); } }, { key: "toList", value: function toList(config) { return this.__items__.toList(config); } }, { key: "toMap", value: function toMap(config) { return this.__items__.toMap(config); } }, { key: "valueType", get: function get() { return this.__items__.valueType; } }, { key: "keyType", get: function get() { return this.__items__.keyType; } }, { key: "rawType", get: function get() { return this.__items__.rawType; } }]); return EnumCollectionClass; }(EnumExtensionClass); //# sourceMappingURL=enum-collection.js.map