enum-plus
Version:
A drop-in replacement for native enum. Like native enum but much better!
193 lines • 8.44 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EnumItemClass = void 0;
const global_config_1 = require("./global-config");
const utils_1 = require("./utils");
/**
* - **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 | 表示枚举项键的类型
*/
class EnumItemClass {
/**
* - **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 | 枚举项的可选设置
*/
constructor(key, value, label, raw, options) {
this.key = key;
this.value = value;
this.label = label;
this.raw = raw;
// Should use _label instead of label closure, to make sure it can be serialized correctly
Object.defineProperty(this, '_label', {
value: label,
writable: false,
enumerable: false,
configurable: false,
});
// Use defineProperties instead of direct field, to:
// 1. Make fields readonly
// 2. Preserve getters after serialized/deserialized
Object.defineProperties(this, {
value: {
value,
writable: false,
enumerable: true,
configurable: false,
},
label: {
get: function () {
var _a, _b, _c, _d;
const labelPrefix = (_a = this._options) === null || _a === void 0 ? void 0 : _a.labelPrefix;
const autoLabel = (_c = (_b = this._options) === null || _b === void 0 ? void 0 : _b.autoLabel) !== null && _c !== void 0 ? _c : global_config_1.internalConfig.autoLabel;
let localeKey = this._label;
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 = `${labelPrefix}${this._label}`;
}
}
return (_d = this._localize(localeKey)) !== null && _d !== void 0 ? _d : localeKey;
},
enumerable: true,
configurable: false,
},
key: {
value: key,
writable: false,
enumerable: true,
configurable: false,
},
raw: {
value: raw,
writable: false,
enumerable: true,
configurable: false,
},
});
// Do not use class field here, because don't want print this field in Node.js
Object.defineProperty(this, '_options', {
value: options,
writable: false,
enumerable: false,
configurable: false,
});
this._localize = undefined;
Object.defineProperty(this, '_localize', {
value: function (content) {
var _a, _b;
const localize = (_b = (_a = this._options) === null || _a === void 0 ? void 0 : _a.localize) !== null && _b !== void 0 ? _b : global_config_1.localizer.localize;
if (typeof localize === 'function') {
return localize(content);
}
return content;
},
writable: false,
enumerable: false,
configurable: false,
});
Object.freeze(this);
}
/**
* - **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
get [utils_1.IS_ENUM_ITEM]() {
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
[Symbol.toPrimitive](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 | 枚举项的显示名称
*/
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 | 枚举项的本地化显示名称
*/
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 | 枚举项的值
*/
valueOf() {
return this.value;
}
}
exports.EnumItemClass = EnumItemClass;
//# sourceMappingURL=enum-item.js.map