UNPKG

enum-plus

Version:

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

111 lines (107 loc) 5.7 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 _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 { EnumCollectionClass, EnumExtensionClass } from "./enum-collection"; import { defaultLocalize } from "./utils"; var enumExtensions; /** * **EN:** Generate an enum collection, the enum value supports `number` and `string` types, and the * enum name supports localization solutions * * **CN:** 生成一个枚举集合,枚举值支持`number`和`string`类型,枚举名称支持本地化方案 * * @example * const Week = Enum({ * Sunday: { value: 0, label: 'Sunday' }, * Monday: { value: 1, label: 'Monday' }, * } as const); * * @param init the init object, see usage examples for the way to pass values | 初始化对象,传值方式参见使用示例 * * @returns Enum collection | 枚举集合 */ /** * **EN:** Generate an enum based on an object array * * **CN:** 基于对象数组生成枚举 * * @example * const Week = Enum([ * { value: 0, label: 'Sunday', key: 'Sun' }, * { value: 1, label: 'Monday', key: 'Mon' }, * ] as const); * * @param init Init objects array | 初始化对象数组 * @param options Generate options | 生成选项 * * @returns Enum collection | 枚举集合 */ export function Enum(init, options) { if (Array.isArray(init)) { var initMap = getInitMapFromArray(init, options); // @ts-expect-error: because of typing extend in tests return new EnumCollectionClass(initMap, options); } else { // @ts-expect-error: because of typing extend in tests return new EnumCollectionClass(init, options); } } /** * **EN:** Global localization function, used to convert enum item text to localized text. Only need * to be set once, effective globally. This method need to be set at the project entry, before * running any Enum instance * * If you want to provide a custom localization function, be sure to resolve and localize the * following built-in resources: * * - `enum-plus.options.all` => `All` * * **CN:** 全局本地化函数,用于把枚举项文本转换为本地化文本。只需要设置一次,全局生效。需要在项目入口处设置,且在运行任何Enum实例之前 * * 如果要提供自定义的本地化函数,请务必解析并本地化以下内置资源: * * - `enum-plus.options.all` => `All` */ Enum.localize = defaultLocalize; /** * **EN:** Add global extension methods to the enum, and all enum instances will have these new * extension methods * * **CN:** 为枚举增加全局扩展方法,所有枚举实例都会具有这些新扩展方法 * * @param obj Extension content, must be an object | 扩展内容,必须是一个对象 */ Enum.extends = function (obj) { if (obj !== undefined && Object.prototype.toString.call(obj) !== '[object Object]') { throw new Error('The extension of Enum must be an object'); } enumExtensions = obj !== undefined ? obj : {}; Object.setPrototypeOf(EnumExtensionClass.prototype, enumExtensions); }; function getInitMapFromArray(init, options) { var _ref = options !== null && options !== void 0 ? options : {}, _ref$getValue = _ref.getValue, getValue = _ref$getValue === void 0 ? 'value' : _ref$getValue, _ref$getLabel = _ref.getLabel, getLabel = _ref$getLabel === void 0 ? 'label' : _ref$getLabel, _ref$getKey = _ref.getKey, getKey = _ref$getKey === void 0 ? 'key' : _ref$getKey; return init.reduce(function (acc, item) { var _key, _key2; var value = typeof getValue === 'function' ? getValue(item) : item[getValue]; var label = typeof getLabel === 'function' ? getLabel(item) : item[getLabel]; var key = undefined; if (getKey) { key = typeof getKey === 'function' ? getKey(item) : item[getKey]; } acc[(_key = key) !== null && _key !== void 0 ? _key : value] = _objectSpread(_objectSpread({}, item), {}, { label: label || ((_key2 = key) !== null && _key2 !== void 0 ? _key2 : '') || (value != null ? value.toString() : value), value: value }); return acc; }, {}); } //# sourceMappingURL=enum.js.map