UNPKG

enum-plus

Version:

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

245 lines (242 loc) 12.6 kB
var _Symbol$toPrimitive; 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 _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 _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 _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); } import { internalConfig, localizer } from "./global-config.js"; import { IS_ENUM_ITEM } from "./utils.js"; _Symbol$toPrimitive = Symbol.toPrimitive; /** * - **EN:** Represents a single item in an enumeration collection. * - **CN:** 表示枚举集合中的单个枚举项 * * @template T Represents the type of the enum item's initialization object | 表示枚举项初始化对象的类型 * @template V Represents the type of the enum item's value (usually string or number) | * 表示枚举项值的类型(通常是字符串或数字) * @template K Represents the type of the enum item's key | 表示枚举项键的类型 * @template LP Represents the type of the enum item's label prefix | 表示枚举项标签的前缀值 */ export var EnumItemClass = /*#__PURE__*/function () { /** * - **EN:** Creates an instance of EnumItemClass. * - **CN:** 创建 EnumItemClass 的实例 * * @param key The key of the enum item | 枚举项键 * @param value The value of the enum item | 枚举项值 * @param label The display name of the enum item | 枚举项显示名称 * @param raw The original initialization object | 原始初始化对象 * @param options Optional settings for the enum item | 枚举项的可选设置 */ function EnumItemClass(key, value, label, raw, options) { var _this = this; _classCallCheck(this, EnumItemClass); // eslint-disable-next-line @typescript-eslint/no-explicit-any _defineProperty(this, "_options", void 0); _defineProperty(this, "_label", void 0); /** * - **EN:** The value of the enum item * - **CN:** 枚举项的值 */ _defineProperty(this, "value", void 0); /** * - **EN:** The label of the enum item (also known as display name) * - **CN:** 枚举项的标签(亦称显示名称) */ _defineProperty(this, "label", void 0); /** * - **EN:** The key of the enum item, which is the key in the initialization object when creating * the enum collection. * - **CN:** 枚举项的键,即创建枚举集合时初始化对象中的键 */ _defineProperty(this, "key", void 0); /** * - **EN:** The original initialization object of the enum item, which is the sub-object of a * single enum item when creating the enum collection. * - **CN:** 枚举项的原始初始化对象,即创建枚举集合时单个枚举项的子对象 */ _defineProperty(this, "raw", void 0); this.key = key; this.value = value; this.label = label; this.raw = raw; var defines = Object.defineProperties; var freeze = Object.freeze; defines(this, { _label: { value: label }, _options: { value: options } }); // Determines whether a property should be auto localized based on the autoLocalizeMeta option var autoLocalizePropMap = {}; if (_typeof(raw) === 'object') { var autoLocalizeMeta = options === null || options === void 0 ? void 0 : options.autoLocalizeMeta; Object.keys(raw).forEach(function (metaKey) { if (!['value', 'label'].includes(metaKey)) { if (autoLocalizeMeta === true || Array.isArray(autoLocalizeMeta) && autoLocalizeMeta.includes(metaKey)) { var descriptor = { // eslint-disable-next-line @typescript-eslint/no-explicit-any get: function get() { // @ts-expect-error: because _metaKey is dynamically added to the getter function var _metaKey = get._metaKey; // eslint-disable-next-line @typescript-eslint/no-explicit-any return this._localizeResource(this.raw[_metaKey]); }, enumerable: true }; autoLocalizePropMap[metaKey] = descriptor; // @ts-expect-error: because _metaKey is dynamically added to the getter function descriptor.get._metaKey = metaKey; } else { _this[metaKey] = raw[metaKey]; } } }); } // Define getters to localize i18n key into localized text defines(this, _objectSpread(_objectSpread({}, autoLocalizePropMap), {}, { label: { get: function get() { return this._localizeResource(this._label); }, enumerable: true } })); freeze(this); freeze(EnumItemClass.prototype); } _createClass(EnumItemClass, [{ key: IS_ENUM_ITEM, get: /** * - **EN:** A boolean value indicates that this is an enum item 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 function get() { return true; } /** * - **EN:** Auto convert to a correct primitive type. This method is called when the object is used * in a context that requires a primitive value. * * > The priority of this method is higher than both `valueOf` and `toString` methods. * * - **CN:** 自动转换为正确的原始类型。当对象被用在需要原始值的上下文中时会调用此方法。 * * > 此方法的优先级高于 `valueOf` 和 `toString` 方法。 * * @param hint {'number' | 'string' | 'default'} - A string indicating the preferred type of the * result | 指示结果的首选类型 * * @returns The primitive value of the enum item, either its value or label based on the hint | * 枚举项的原始值,根据提示返回其值或标签 */ // @ts-expect-error: because don't want show `Symbol` in vscode's intellisense, it should work in background }, { key: _Symbol$toPrimitive, value: function value(hint) { if (hint === 'number') { // for the cases like Number(value) or +value return this.valueOf(); } else if (hint === 'string') { // for the cases like String(value), `${value}` return this.toString(); } // for the cases like '' + value, value == 1 return this.valueOf(); } /** * - **EN:** Returns the string representation of the enum item, which is its label. This method is * called when the object is used in a context that requires a string value, such as string * concatenation or template literals. * * > The priority of this method is lower than the `valueOf` method * * - **CN:** 返回枚举项的字符串表示形式,即其显示名称。当对象被用在需要字符串值的上下文中时会调用此方法,例如字符串连接或模板字面量。 * * > 此方法的优先级低于 `valueOf` 方法 * * @returns The display name of the enum item | 枚举项的显示名称 */ }, { key: "toString", value: function toString() { return this.label; } /** * - **EN:** Returns the localized string representation of the enum item, which is its label. This * method is called when the object is used in a context that requires a localized string value, * such as `toLocaleString` method. * - **CN:** 返回枚举项的本地化字符串表示形式,即其显示名称。当对象被用在需要本地化字符串值的上下文中时会调用此方法,例如 `toLocaleString` 方法。 * * @returns The localized display name of the enum item | 枚举项的本地化显示名称 */ }, { key: "toLocaleString", value: function toLocaleString() { return this.toString(); } /** * - **EN:** Returns the primitive value of the enum item, which is its value. This method is called * when the object is used in a context that requires a primitive value, such as mathematical * operations. * * > The priority of this method is higher than the `toString` method * * - **CN:** 返回枚举项的原始值,即其值。当对象被用在需要原始值的上下文中时会调用此方法,例如数学运算。 * * > 此方法的优先级高于 `toString` 方法 * * @returns The value of the enum item | 枚举项的值 */ }, { key: "valueOf", value: function valueOf() { return this.value; } }, { key: "_localize", value: function _localize(content) { var _this$_options$locali, _this$_options; var localize = (_this$_options$locali = (_this$_options = this._options) === null || _this$_options === void 0 ? void 0 : _this$_options.localize) !== null && _this$_options$locali !== void 0 ? _this$_options$locali : localizer.localize; if (typeof localize === 'function') { return localize(content); } return content; } }, { key: "_localizeResource", value: function _localizeResource(resource) { var _this$_options2, _this$_options$autoLa, _this$_options3, _this$_localize; var labelPrefix = (_this$_options2 = this._options) === null || _this$_options2 === void 0 ? void 0 : _this$_options2.labelPrefix; var autoLabel = (_this$_options$autoLa = (_this$_options3 = this._options) === null || _this$_options3 === void 0 ? void 0 : _this$_options3.autoLabel) !== null && _this$_options$autoLa !== void 0 ? _this$_options$autoLa : internalConfig.autoLabel; var localeKey = resource; if (typeof localeKey === 'function') { // eslint-disable-next-line @typescript-eslint/no-explicit-any return localeKey(this); } if (autoLabel && labelPrefix != null) { if (typeof autoLabel === 'function') { localeKey = autoLabel({ item: this, labelPrefix: labelPrefix }); } else { localeKey = "".concat(labelPrefix).concat(resource); } } return (_this$_localize = this._localize(localeKey)) !== null && _this$_localize !== void 0 ? _this$_localize : localeKey; } }]); return EnumItemClass; }(); //# sourceMappingURL=enum-item.js.map