@coreui/vue-pro
Version:
UI Components Library for Vue.js
111 lines (108 loc) • 3.98 kB
JavaScript
;
const filterOptions = (options, search) => {
const result = [];
options.forEach((item) => {
if (typeof item !== 'string' && 'options' in item && Array.isArray(item.options)) {
const filteredOptions = filterOptions(item.options, search);
if (filteredOptions.length > 0) {
result.push({ ...item, options: filteredOptions });
}
}
else if (getOptionLabel(item).toLowerCase().includes(search.toLowerCase())) {
result.push(item);
}
});
return result;
};
const flattenOptionsArray = (options, keepGroupLabel) => {
const optionsList = [];
for (const option of options) {
if (typeof option !== 'string' && 'options' in option && Array.isArray(option.options)) {
optionsList.push(...option.options);
}
else {
optionsList.push(option);
}
}
return optionsList;
};
const isExternalSearch = (search) => {
return ((typeof search === 'string' && search === 'external') ||
(typeof search === 'object' && search.external === true));
};
const isGlobalSearch = (search) => {
return ((typeof search === 'string' && search === 'global') ||
(typeof search === 'object' && search.global === true));
};
const getOptionLabel = (option) => typeof option === 'string' ? option : option.label;
const ESCAPE_HTML_MAP = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
};
const escapeHtml = (string) => String(string).replaceAll(/[&<>"']/g, (character) => ESCAPE_HTML_MAP[character]);
const escapeRegExp = (string) => String(string).replaceAll(/[.*+?^${}()|[\]\\]/g, String.raw `\$&`);
const highlightSubstring = (string, query) => {
if (!query) {
return escapeHtml(string);
}
const regex = new RegExp(`(${escapeRegExp(query)})`, 'gi');
return String(string)
.split(regex)
.map((part, index) => index % 2 === 0 ? escapeHtml(part) : `<strong>${escapeHtml(part)}</strong>`)
.join('');
};
const isOptionDisabled = (option) => typeof option === 'string' ? false : option.disabled;
const isOptionSelected = (option, selected) => {
if (!selected) {
return false;
}
if (typeof option === 'string' && typeof selected === 'string') {
return selected === option;
}
if (typeof option !== 'string' && typeof selected !== 'string') {
return selected.label === option.label;
}
return false;
};
const getFirstOptionByLabel = (value, options) => {
for (const option of options) {
if (typeof option !== 'string' && 'options' in option && Array.isArray(option.options)) {
const found = getFirstOptionByLabel(value, option.options);
if (found) {
return found;
}
}
if (getOptionLabel(option).toLowerCase() === value.toLowerCase()) {
return option;
}
}
return null;
};
const getFirstOptionByValue = (value, options) => {
for (const option of options) {
if (typeof option !== 'string' && 'options' in option && Array.isArray(option.options)) {
const found = getFirstOptionByValue(value, option.options);
if (found) {
return found;
}
}
if (typeof option !== 'string' && 'value' in option && option.value === value) {
return option;
}
}
return null;
};
exports.filterOptions = filterOptions;
exports.flattenOptionsArray = flattenOptionsArray;
exports.getFirstOptionByLabel = getFirstOptionByLabel;
exports.getFirstOptionByValue = getFirstOptionByValue;
exports.getOptionLabel = getOptionLabel;
exports.highlightSubstring = highlightSubstring;
exports.isExternalSearch = isExternalSearch;
exports.isGlobalSearch = isGlobalSearch;
exports.isOptionDisabled = isOptionDisabled;
exports.isOptionSelected = isOptionSelected;
//# sourceMappingURL=utils.js.map