smart-array-filter
Version:
Filter an array of objects
34 lines • 1.03 kB
JavaScript
import recursiveMatch from './recursiveMatch';
/**
* Match.
* @param element - String | number | Record<string, string>.
* @param criteria - Criterion[].
* @param predicate - String.
* @param options - Object.
* @param options.ignorePaths - RegExp[].
* @param options.pathAlias - Record<string, string|RegExp>s.
* @param options.includePaths
* @returns Boolean.
*/
export default function match(element, criteria, predicate, options) {
if (criteria.length > 0) {
let found = false;
for (const criterion of criteria) {
// match XOR negate
if (recursiveMatch(element, criterion, [], options)
? !criterion.negate
: criterion.negate) {
if (predicate === 'OR') {
return true;
}
found = true;
}
else if (predicate === 'AND') {
return false;
}
}
return found;
}
return true;
}
//# sourceMappingURL=match.js.map