UNPKG

@deepkit/core

Version:
118 lines 3.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getValidEnumValue = exports.isValidEnumValue = exports.getEnumKeyLabelMap = exports.getEnumValues = exports.getEnumLabels = exports.getEnumLabel = void 0; 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. */ const iterators_js_1 = require("./iterators.js"); const cacheEnumLabels = new Map(); /** * Returns the enum label for a given enum value. * * @public */ function getEnumLabel(enumType, id) { for (const i of (0, iterators_js_1.eachKey)(enumType)) { if (id === enumType[i]) { return i; } } } exports.getEnumLabel = getEnumLabel; getEnumLabel.__type = ['enumType', 'id', 'getEnumLabel', 'PP&"LM2!"2""/#']; /** * Returns all possible enum labels. * * @public */ 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; } exports.getEnumLabels = getEnumLabels; getEnumLabels.__type = ['enumDefinition', 'getEnumLabels', 'P"2!"/"']; const cacheEnumKeys = new Map(); /** * Returns all possible enum keys. * * @public */ 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; } exports.getEnumValues = getEnumValues; getEnumValues.__type = ['enumDefinition', 'getEnumValues', 'P"2!"F/"']; function getEnumKeyLabelMap(enumDefinition) { const 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; } exports.getEnumKeyLabelMap = getEnumKeyLabelMap; getEnumKeyLabelMap.__type = ['enumDefinition', 'getEnumKeyLabelMap', 'P"2!"&E/"']; /** * Checks whether given enum value is valid. * * @public */ 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)); } exports.isValidEnumValue = isValidEnumValue; isValidEnumValue.__type = ['enumDefinition', 'value', 'allowLabelsAsValue', 'isValidEnumValue', 'P"2!"2""2#"/$']; /** * @public */ 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); } } exports.getValidEnumValue = getValidEnumValue; getValidEnumValue.__type = ['enumDefinition', 'value', 'allowLabelsAsValue', 'getValidEnumValue', 'P"2!"2""2#"/$']; //# sourceMappingURL=enum.js.map