UNPKG

@esri/calcite-components

Version:

Web Components for Esri's Calcite Design System.

39 lines (38 loc) 1.15 kB
/*! All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://github.com/Esri/calcite-design-system/blob/dev/LICENSE.md for details. v3.2.1 */ import { escapeRegExp, forIn } from "lodash-es"; const filter = (data, value, filterProps) => { 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, fields) => { if (input?.constant || input?.filterDisabled) { return true; } let found = false; forIn(input, (val, key) => { if (typeof val === "function" || val == null) { return; } if (fields && !fields.includes(key)) { 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; }; return data.filter((item) => find(item, regex, filterProps)); }; export { filter as f };