UNPKG

@deepkit/core

Version:

Deepkit core library

108 lines 4.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getEnumLabel = getEnumLabel; exports.getEnumLabels = getEnumLabels; exports.getEnumValues = getEnumValues; exports.getEnumKeyLabelMap = getEnumKeyLabelMap; exports.isValidEnumValue = isValidEnumValue; exports.getValidEnumValue = getValidEnumValue; const __ΩObject = [() => Function, 'constructor', 'toString', 'toLocaleString', 0, 'valueOf', () => __ΩPropertyKey, 'v', 'hasOwnProperty', 0, 'isPrototypeOf', () => __ΩPropertyKey, 'propertyIsEnumerable', 'Object', 'PPu!4"P&1#P&1$Pn%1&Pn\'2()1)Pn*2()1+Pn,2()1-Mw.y']; const __ΩPropertyKey = ['PropertyKey', 'P&\'+Jw!y']; function __assignType(fn, args) { fn.__type = args; return fn; } /* * Deepkit Framework * Copyright (C) 2021 Deepkit UG, Marc J. Schmidt * * This program is free software: you can redistribute it and/or modify * it under the terms of the MIT License. * * You should have received a copy of the MIT License along with this program. */ /** @group Enum */ const cacheEnumLabels = (Map.Ω = [[() => __ΩObject, 'n!'], ['&F']], new Map()); /** * Returns the enum label for a given enum value. */ function getEnumLabel(enumType, id) { for (const i of Object.values(enumType)) { if (id === enumType[i]) { return i; } } } getEnumLabel.__type = ['enumType', 'id', 'getEnumLabel', 'PP&"LM2!"2""/#']; /** * Returns all possible enum labels. */ function getEnumLabels(enumDefinition) { let value = cacheEnumLabels.get(enumDefinition); if (!value) { value = Object.keys(enumDefinition).filter(__assignType(v => !Number.isFinite(parseInt(v, 10)), ['v', '', 'P"2!"/"'])); cacheEnumLabels.set(enumDefinition, value); } return value; } getEnumLabels.__type = ['enumDefinition', 'getEnumLabels', 'P"2!"/"']; const cacheEnumKeys = (Map.Ω = [[() => __ΩObject, 'n!'], ['&F']], new Map()); /** * Returns all possible enum keys. */ function getEnumValues(enumDefinition) { let value = cacheEnumKeys.get(enumDefinition); if (!value) { value = Object.values(enumDefinition) .filter(__assignType((v) => { return 'number' !== typeof enumDefinition[v]; }, ['v', '', 'P"2!"/"'])); cacheEnumKeys.set(enumDefinition, value); } return value; } getEnumValues.__type = ['enumDefinition', 'getEnumValues', 'P"2!"F/"']; function getEnumKeyLabelMap(enumDefinition) { const map = (Map.Ω = [['"'], ['&']], new Map()); const keys = getEnumValues(enumDefinition); const labels = getEnumLabels(enumDefinition); for (let i = 0; i < keys.length; i++) { map.set(keys[i], labels[i]); } return map; } getEnumKeyLabelMap.__type = ['enumDefinition', 'getEnumKeyLabelMap', 'P"2!"&E/"']; /** * Checks whether given enum value is valid. */ function isValidEnumValue(enumDefinition, value, allowLabelsAsValue = false) { if (allowLabelsAsValue) { const labels = getEnumLabels(enumDefinition); if (-1 !== labels.indexOf(String(value))) { return true; } } const values = getEnumValues(enumDefinition); return -1 !== values.indexOf(+value) || -1 !== values.indexOf(value) || -1 !== values.indexOf(String(value)); } isValidEnumValue.__type = ['enumDefinition', 'value', 'allowLabelsAsValue', 'isValidEnumValue', 'P"2!"2""2#"/$']; function getValidEnumValue(enumDefinition, value, allowLabelsAsValue = false) { if (allowLabelsAsValue) { const labels = getEnumLabels(enumDefinition); if (-1 !== labels.indexOf(String(value))) { return enumDefinition[String(value)]; } } const values = getEnumValues(enumDefinition); if (-1 !== values.indexOf(value)) { return value; } if (-1 !== values.indexOf(+value)) { return +value; } if (-1 !== values.indexOf(String(value))) { return String(value); } } getValidEnumValue.__type = ['enumDefinition', 'value', 'allowLabelsAsValue', 'getValidEnumValue', 'P"2!"2""2#"/$']; //# sourceMappingURL=enum.js.map