open-consul/ui/packages/consul-ui/app/utils/search/fuzzy.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

21 lines
479 B
JavaScript

import Fuse from 'fuse.js';
export default class FuzzySearch {
constructor(items, options) {
this.fuse = new Fuse(items, {
includeMatches: true,
shouldSort: false, // use our own sorting for the moment
threshold: 0.4,
keys: Object.keys(options.finders) || [],
getFn(item, key) {
return (options.finders[key[0]](item) || []).toString();
},
});
}
search(s) {
return this.fuse.search(s).map(item => item.item);
}
}