fuzzy search #246

This commit is contained in:
Jan Prochazka
2022-03-24 15:48:21 +01:00
parent f3bfe58c58
commit 25ae5bf048
2 changed files with 28 additions and 7 deletions

View File

@@ -33,6 +33,27 @@ import _compact from 'lodash/compact';
// return DoMatch(Filter, value) || camelMatch;
// }
function fuzzysearch(needle, haystack) {
var hlen = haystack.length;
var nlen = needle.length;
if (nlen > hlen) {
return false;
}
if (nlen === hlen) {
return needle === haystack;
}
outer: for (var i = 0, j = 0; i < nlen; i++) {
var nch = needle.charCodeAt(i);
while (j < hlen) {
if (haystack.charCodeAt(j++) === nch) {
continue outer;
}
}
return false;
}
return true;
}
export function filterName(filter: string, ...names: string[]) {
if (!filter) return true;
@@ -42,7 +63,7 @@ export function filterName(filter: string, ...names: string[]) {
const namesCompacted = _compact(names);
for (const token of tokens) {
const tokenUpper = token.toUpperCase();
const found = namesCompacted.find(name => name.toUpperCase().includes(tokenUpper));
const found = namesCompacted.find(name => fuzzysearch(tokenUpper, name.toUpperCase()));
if (!found) return false;
}