UNPKG

enum-plus

Version:

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

273 lines (269 loc) 15.5 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); } 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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 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 _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 _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 _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } function _classPrivateFieldGet(receiver, privateMap) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); return _classApplyDescriptorGet(receiver, descriptor); } function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; } function _classPrivateFieldSet(receiver, privateMap, value) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to " + action + " private field on non-instance"); } return privateMap.get(receiver); } function _classApplyDescriptorSet(receiver, descriptor, value) { if (descriptor.set) { descriptor.set.call(receiver, value); } else { if (!descriptor.writable) { throw new TypeError("attempted to set read only private field"); } descriptor.value = value; } } 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>} */ var _raw = /*#__PURE__*/new WeakMap(); var _localize = /*#__PURE__*/new WeakMap(); var _optionsConfigDefaults = /*#__PURE__*/new WeakMap(); export var EnumItemsArray = /*#__PURE__*/function (_ref) { _inherits(EnumItemsArray, _ref); var _super = _createSuper(EnumItemsArray); /** * 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 */ function EnumItemsArray(raw, options) { var _this; _classCallCheck(this, EnumItemsArray); for (var _len = arguments.length, items = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) { items[_key - 2] = arguments[_key]; } _this = _super.call.apply(_super, [this].concat(items)); _classPrivateFieldInitSpec(_assertThisInitialized(_this), _raw, { writable: true, value: void 0 }); _classPrivateFieldInitSpec(_assertThisInitialized(_this), _localize, { writable: true, value: void 0 }); _classPrivateFieldInitSpec(_assertThisInitialized(_this), _optionsConfigDefaults, { writable: true, value: { firstOption: false } }); /** * **EN:** A boolean value indicates that this is an enum items array. * * **CN:** 布尔值,表示这是一个枚举项数组 */ _defineProperty(_assertThisInitialized(_this), ENUM_ITEMS, true); _classPrivateFieldSet(_assertThisInitialized(_this), _raw, raw); _classPrivateFieldSet(_assertThisInitialized(_this), _localize, function (content) { var _options$localize; var localize = (_options$localize = options === null || options === void 0 ? void 0 : options.localize) !== null && _options$localize !== void 0 ? _options$localize : Enum.localize; if (typeof localize === 'function') { return localize(content); } return content; }); return _this; } _createClass(EnumItemsArray, [{ key: "label", value: function label(keyOrValue) { var _ref2, _this$find; // First find by value, then find by key return (_ref2 = (_this$find = this.find(function (i) { return i.value === keyOrValue; })) !== null && _this$find !== void 0 ? _this$find : this.find(function (i) { return i.key === keyOrValue; })) === null || _ref2 === void 0 ? void 0 : _ref2.label; } }, { key: "key", value: function key(value) { var _this$find2; return (_this$find2 = this.find(function (i) { return i.value === value; })) === null || _this$find2 === void 0 ? void 0 : _this$find2.key; } }, { key: "has", value: function has(keyOrValue) { return this.some(function (i) { return i.value === keyOrValue || i.key === keyOrValue; }); } }, { key: "toSelect", value: function toSelect() { var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _classPrivateFieldGet(this, _optionsConfigDefaults); var _config$firstOption = config.firstOption, firstOption = _config$firstOption === void 0 ? _classPrivateFieldGet(this, _optionsConfigDefaults).firstOption : _config$firstOption; if (firstOption) { if (firstOption === true) { var _ref3, _ref4; // 默认选项 var value = (_ref3 = 'firstOptionValue' in config ? config.firstOptionValue : undefined) !== null && _ref3 !== void 0 ? _ref3 : ''; var label = (_ref4 = 'firstOptionLabel' in config ? config.firstOptionLabel : undefined) !== null && _ref4 !== void 0 ? _ref4 : 'enum-plus.options.all'; return [{ key: '', value: value, label: _classPrivateFieldGet(this, _localize).call(this, label) }].concat(_toConsumableArray(this)); } else { var _firstOption$key; return [_objectSpread(_objectSpread({}, firstOption), {}, { key: (_firstOption$key = firstOption.key) !== null && _firstOption$key !== void 0 ? _firstOption$key : firstOption.value, label: _classPrivateFieldGet(this, _localize).call(this, firstOption.label) })].concat(_toConsumableArray(this)); } } else { return this; } } /** @deprecated Use `toSelect` instead */ }, { key: "options", value: /** @deprecated Use `toSelect` instead */ function options(config) { return this.toSelect(config); } }, { key: "toValueMap", value: function toValueMap() { var itemsMap = {}; for (var i = 0; i < this.length; i++) { var _this$i = this[i], value = _this$i.value, label = _this$i.label; itemsMap[value] = { text: label }; } return itemsMap; } /** @deprecated Use `toValueMap` instead */ }, { key: "valuesEnum", value: function valuesEnum() { return this.toValueMap(); } }, { key: "toMenu", value: function toMenu() { return this.map(function (_ref5) { var value = _ref5.value, label = _ref5.label; return { key: value, label: label }; }); } /** @deprecated Use `toMenu` instead */ }, { key: "menus", value: function menus() { return this.toMenu(); } }, { key: "toFilter", value: function toFilter() { return this.map(function (_ref6) { var value = _ref6.value, label = _ref6.label; return { text: label, value: value }; }); } /** @deprecated Use `toFilter` instead */ }, { key: "filters", value: function filters() { return this.toFilter(); } }, { key: "raw", value: function raw(value) { if (value == null) { // Return the original initialization object return _classPrivateFieldGet(this, _raw); } else { if (Object.keys(_classPrivateFieldGet(this, _raw)).some(function (k) { return k === value; })) { // Find by key return _classPrivateFieldGet(this, _raw)[value]; } // Find by value var itemByValue = this.find(function (i) { return i.value === value; }); if (itemByValue) { return itemByValue.raw; } else { return undefined; } } } /** @deprecated Stub method, only for typing usages, not for runtime calling */ }, { key: "valueType", get: function get() { 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 */ }, { key: "keyType", get: function get() { 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 */ }, { key: "rawType", get: function get() { 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'); } }]); return EnumItemsArray; }(Array); /** @deprecated Use `EnumItemsArray` instead */ export var EnumValuesArray = /*#__PURE__*/function (_EnumItemsArray) { _inherits(EnumValuesArray, _EnumItemsArray); var _super2 = _createSuper(EnumValuesArray); function EnumValuesArray() { _classCallCheck(this, EnumValuesArray); return _super2.apply(this, arguments); } return _createClass(EnumValuesArray); }(EnumItemsArray); //# sourceMappingURL=enum-values.js.map