enum-plus
Version:
A drop-in replacement for native enum. Like native enum but much better!
170 lines (168 loc) • 8.54 kB
JavaScript
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 _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 _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 _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 { localizer } from "./localize.js";
import { ENUM_ITEM } from "./utils.js";
/**
* Enum item class
*
* @template V General type for item value
* @template K General type for item key
* @template T General type for item initialization object
*/
var _localize = /*#__PURE__*/new WeakMap();
var _localizedProxy = /*#__PURE__*/new WeakMap();
export var EnumItemClass = /*#__PURE__*/function () {
/**
* Instantiate an enum item
*
* @param key Enum item key
* @param value Enum item value
* @param label Enum item display name
* @param raw Original initialization object
* @param options Construction options
*/
function EnumItemClass(key, value, label, raw, options) {
var _this = this;
_classCallCheck(this, EnumItemClass);
/**
* **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);
/**
* **EN:** A boolean value indicates that this is an enum item instance.
*
* **CN:** 布尔值,表示这是一个枚举项实例
*/
_defineProperty(this, ENUM_ITEM, true);
_classPrivateFieldInitSpec(this, _localize, {
writable: true,
value: void 0
});
_classPrivateFieldInitSpec(this, _localizedProxy, {
writable: true,
value: new Proxy(this, {
get: function get(target, prop) {
var origin = target[prop];
if (prop === 'label') {
return target.toString();
} else if (typeof origin === 'function') {
return origin.bind(target);
}
return origin;
},
// Not allowed to edit
set: function set(_, prop) {
/* istanbul ignore if */
if (!process.env.JEST_WORKER_ID) {
console.warn("Cannot modify property \"".concat(String(prop), "\" on EnumItem. EnumItem instances are readonly and should not be mutated."));
}
return true;
},
defineProperty: function defineProperty(_, prop) {
/* istanbul ignore if */
if (!process.env.JEST_WORKER_ID) {
console.warn("Cannot modify property \"".concat(String(prop), "\" on EnumItem. EnumItem instances are readonly and should not be mutated."));
}
return true;
},
deleteProperty: function deleteProperty(_, prop) {
/* istanbul ignore if */
if (!process.env.JEST_WORKER_ID) {
console.warn("Cannot modify property \"".concat(String(prop), "\" on EnumItem. EnumItem instances are readonly and should not be mutated."));
}
return true;
},
setPrototypeOf: function setPrototypeOf() {
/* istanbul ignore if */
if (!process.env.JEST_WORKER_ID) {
console.warn('Cannot change prototype of EnumItem. EnumItem instances are immutable.');
}
return true;
}
})
});
this.key = key;
this.value = value;
this.label = label;
this.raw = raw;
_classPrivateFieldSet(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 : localizer.localize;
if (typeof localize === 'function') {
return localize(content);
}
return content;
});
// @ts-expect-error: because override Object.toPrimitive method to return enum value
this[Symbol.toPrimitive] = function (hint) {
if (hint === 'number') {
// for cases like Number(value) or +value
return _this.valueOf();
} else if (hint === 'string') {
// for cases like String(value), `${value}`
return _this.toString();
}
// for cases like '' + value, value == 1
return _this.valueOf();
};
// Object.freeze(this);
}
_createClass(EnumItemClass, [{
key: "readonly",
value: function readonly() {
return _classPrivateFieldGet(this, _localizedProxy);
}
}, {
key: "toString",
value: function toString() {
var _classPrivateFieldGet2;
return (_classPrivateFieldGet2 = _classPrivateFieldGet(this, _localize).call(this, this.label)) !== null && _classPrivateFieldGet2 !== void 0 ? _classPrivateFieldGet2 : this.label;
}
}, {
key: "toLocaleString",
value: function toLocaleString() {
return this.toString();
}
}, {
key: "valueOf",
value: function valueOf() {
return this.value;
}
}]);
return EnumItemClass;
}();
//# sourceMappingURL=enum-item.js.map