UNPKG

@ncoderz/superenum

Version:

Simple, typesafe enums in TypeScript, fully compatible with standard JavaScript

197 lines (195 loc) 6.06 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { Enum: () => Enum }); module.exports = __toCommonJS(index_exports); // src/superenum.ts var CACHED_ENUMS = /* @__PURE__ */ new WeakMap(); function Enum(enm) { let _cache = CACHED_ENUMS.get(enm); const enmAny = enm; if (!_cache) { const enmKeys = []; for (const key in enm) { const nkey = Number(key); const isReverseKey = !isNaN(nkey) && enmAny[enmAny[nkey]] === nkey; if (!isReverseKey) { enmKeys.push(key); } } const newCache = { _keys: enmKeys, _valueKeyMap: /* @__PURE__ */ new Map() }; for (const key of newCache._keys) { const value = enmAny[key]; newCache._valueKeyMap.set(value, key); } CACHED_ENUMS.set(enm, newCache); _cache = newCache; } function createKeyValueMap() { const newMap = /* @__PURE__ */ new Map(); for (const key of _cache._keys) { const value = enmAny[key]; newMap.set(key, value); } _cache._keyValueMap = newMap; return newMap; } function createLowerCaseValueKeyMap() { const newMap = /* @__PURE__ */ new Map(); for (const key of _cache._keys) { const value = enmAny[key]; const lcValue = typeof value === "string" ? value.toLowerCase() : value; newMap.set(lcValue, key); } _cache._lcValueKeyMap = newMap; return newMap; } function createLowerCaseKeyValueMap() { const newMap = /* @__PURE__ */ new Map(); for (const key of _cache._keys) { const value = enmAny[key]; newMap.set(key.toLowerCase(), value); } _cache._lcKeyValueMap = newMap; return newMap; } function createValueLabelMap() { const newMap = /* @__PURE__ */ new Map(); _cache._valueLabelMap = newMap; return newMap; } function createValues() { const keyValueMap = _cache._keyValueMap ?? createKeyValueMap(); _cache._values = _cache._keys.map((k) => keyValueMap.get(k)); return _cache._values; } function createEntries() { const keyValueMap = _cache._keyValueMap ?? createKeyValueMap(); _cache._entries = _cache._keys.map((k) => [k, keyValueMap.get(k)]); return _cache._entries; } function fromValue(value, options) { if (options?.ignoreCase && typeof value === "string") { const keyValueMap = _cache._keyValueMap ?? createKeyValueMap(); const lcValueKeyMap = _cache._lcValueKeyMap ?? createLowerCaseValueKeyMap(); const key = lcValueKeyMap.get(value.toLowerCase()); if (!key) return void 0; return keyValueMap.get(key); } if (!_cache._valueKeyMap.has(value)) return void 0; return value; } function fromKey(key, options) { const keyValueMap = _cache._keyValueMap ?? createKeyValueMap(); if (options?.ignoreCase && typeof key === "string") { const lcKeyValueMap = _cache._lcKeyValueMap ?? createLowerCaseKeyValueMap(); return lcKeyValueMap.get(key.toLowerCase()); } return keyValueMap.get(`${key}`); } function keyFromValue(value, options) { if (options?.ignoreCase && typeof value === "string") { const lcValueKeyMap = _cache._lcValueKeyMap ?? createLowerCaseValueKeyMap(); return lcValueKeyMap.get(value.toLowerCase()); } return _cache._valueKeyMap.get(value); } function hasKey(key, options) { return fromKey(key, options) != null; } function hasValue(value, options) { return fromValue(value, options) != null; } function keys() { return _cache._keys; } function values() { return _cache._values ?? createValues(); } function entries() { return _cache._entries ?? createEntries(); } function* valueIterator() { for (const v of values()) { yield v; } } function setAllLabels(allLabels) { const valueLabelMap = _cache._valueLabelMap ?? createValueLabelMap(); for (const [v, labels] of Object.entries(allLabels)) { const value = fromValue(v); if (value != null) { valueLabelMap.set(value, labels); } } } function setLabels(value, labels) { const valueLabelMap = _cache._valueLabelMap ?? createValueLabelMap(); valueLabelMap.set(value, labels); } function getLabels(value) { const valueLabelMap = _cache._valueLabelMap ?? createValueLabelMap(); return valueLabelMap.get(value) ?? {}; } function getLabel(value, locale) { const valueLabelMap = _cache._valueLabelMap ?? createValueLabelMap(); const labels = valueLabelMap.get(value) ?? {}; if (!locale) { for (const label of Object.values(labels)) { return label ?? `${value}`; } } return labels[locale] ?? `${value}`; } return { fromValue, fromKey, keyFromValue, hasKey, hasValue, keys: () => keys(), values: () => values(), entries: () => entries(), [Symbol.iterator]: valueIterator, setAllLabels, setLabels, getLabels, getLabel }; } Enum.fromArray = (enumeration) => { let arr = enumeration; if (!Array.isArray(arr)) arr = []; const enm = arr.reduce((acc, v) => { acc[v] = v; return acc; }, {}); return enm; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Enum }); //# sourceMappingURL=index.cjs.map