UNPKG

kwikid-components-react

Version:

KwikID's Component Library in React

48 lines (47 loc) 2.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.transformOptions = exports.generateRandomElementId = exports.findOptionWithValueOrLabel = void 0; var _kwikidToolkit = require("kwikid-toolkit"); const transformOptions = options => { const transformedOptions = options.filter(Boolean).map(item => { if (Array.isArray(item)) { return transformOptions(item); } if (typeof item === "string" || typeof item === "number" || typeof item === "boolean") { return { label: item, value: item, disabled: false, description: "", key: item.toString().split(" ").join("_").toLocaleLowerCase() }; } return { ...item, label: item !== null && item !== void 0 && item.label ? item.label : item.value, value: item !== null && item !== void 0 && item.value ? item.value : item.label, disabled: item && item.disabled ? item.disabled : false, description: item && item.description ? item.description : "", key: item && item.key ? item.key : item.label.toString().split(" ").join("_").toLocaleLowerCase() }; }); return (0, _kwikidToolkit.getObjectDeepCopy)(transformedOptions); }; exports.transformOptions = transformOptions; const findOptionWithValueOrLabel = (options, value) => { const transformedOptions = transformOptions(options); if (Array.isArray(value)) { return transformedOptions.filter(option => value.includes(option.value) || value.includes(option.label)); } return transformedOptions.find(option => (option === null || option === void 0 ? void 0 : option.value) === value || (option === null || option === void 0 ? void 0 : option.label) === value); }; exports.findOptionWithValueOrLabel = findOptionWithValueOrLabel; const generateRandomElementId = () => { const RADIX = 36; const SUBSTR_START = 2; const SUBSTR_END = 9; return crypto.randomUUID ? crypto.randomUUID() : Math.random().toString(RADIX).substring(SUBSTR_START, SUBSTR_END); }; exports.generateRandomElementId = generateRandomElementId;