@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
39 lines (38 loc) • 1.14 kB
JavaScript
/*!
* All material copyright ESRI, All Rights Reserved, unless otherwise specified.
* See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
* v1.5.0-next.4
*/
import { escapeRegExp, forIn } from "lodash-es";
export const filter = (data, value) => {
const escapedValue = escapeRegExp(value);
const regex = new RegExp(escapedValue, "i");
if (data.length === 0) {
console.warn(`No data was passed to the filter function.
The data argument should be an array of objects`);
}
const find = (input, RE) => {
if (input?.constant || input?.filterDisabled) {
return true;
}
let found = false;
forIn(input, (val) => {
if (typeof val === "function" || val == null /* intentional == to catch undefined */) {
return;
}
if (Array.isArray(val) || (typeof val === "object" && val !== null)) {
if (find(val, RE)) {
found = true;
}
}
else if (RE.test(val)) {
found = true;
}
});
return found;
};
const result = data.filter((item) => {
return find(item, regex);
});
return result;
};