open-consul/ui/packages/consul-ui/app/utils/search/predicate.js
John Cowen 5dbfbf0049
ui: Fuzzy and Regex searching (#9424)
Moves search things around to match an interface that can be switched in and out of fuzzy searching using fuse.js. We add both fuzzy searching and regex based searching to the codebase here, but it is not yet compiled in.
2020-12-18 10:38:15 +00:00

23 lines
592 B
JavaScript

export default class PredicateSearch {
constructor(items, options) {
this.items = items;
this.options = options;
}
search(s) {
const predicate = this.predicate(s);
// Test the value of each key for each object against the regex
// All that match are returned.
return this.items.filter(item => {
return Object.entries(this.options.finders).some(([key, finder]) => {
const val = finder(item);
if (Array.isArray(val)) {
return val.some(predicate);
} else {
return predicate(val);
}
});
});
}
}