2023-03-14 13:18:55 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2020-12-18 10:38:15 +00:00
|
|
|
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.
|
2022-09-15 08:43:17 +00:00
|
|
|
return this.items.filter((item) => {
|
2020-12-18 10:38:15 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|