UNPKG

enum-plus

Version:

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

46 lines 1.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Enum = void 0; const enum_collection_1 = require("./enum-collection"); const localize_1 = require("./localize"); let enumExtensions; exports.Enum = ((init, options) => { if (Array.isArray(init)) { const initMap = getInitMapFromArray(init, options); return new enum_collection_1.EnumCollectionClass(initMap, options); } else { return new enum_collection_1.EnumCollectionClass(init, options); } }); /* Get or set the global localization function. Use defineProperty here to prevent circular dependencies. */ Object.defineProperty(exports.Enum, 'localize', { get: () => localize_1.localizer.localize, set: (localize) => { localize_1.localizer.localize = localize; }, }); exports.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(enum_collection_1.EnumExtensionClass.prototype, enumExtensions); }; function getInitMapFromArray(init, options) { const { getValue = 'value', getLabel = 'label', getKey = 'key' } = options !== null && options !== void 0 ? options : {}; return init.reduce((acc, item) => { const value = typeof getValue === 'function' ? getValue(item) : item[getValue]; const label = typeof getLabel === 'function' ? getLabel(item) : item[getLabel]; let key = undefined; if (getKey) { key = typeof getKey === 'function' ? getKey(item) : item[getKey]; } acc[(key !== null && key !== void 0 ? key : value)] = Object.assign(Object.assign({}, item), { label: label || (key !== null && key !== void 0 ? key : '') || (value != null ? value.toString() : value), value }); return acc; }, {}); } //# sourceMappingURL=enum.js.map