Skip to content

Commit

Permalink
fuzzy search #246
Browse files Browse the repository at this point in the history
  • Loading branch information
janproch committed Mar 24, 2022
1 parent f3bfe58 commit 25ae5bf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
12 changes: 6 additions & 6 deletions packages/api/.env
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ DEVMODE=1
# HIDE_APP_EDITOR=1


DEVWEB=1
LOGINS=admin,test
# DEVWEB=1
# LOGINS=admin,test

LOGIN_PASSWORD_admin=admin
LOGIN_PERMISSIONS_admin=*
# LOGIN_PASSWORD_admin=admin
# LOGIN_PERMISSIONS_admin=*

LOGIN_PASSWORD_test=test
LOGIN_PERMISSIONS_test=~*, widgets/database
# LOGIN_PASSWORD_test=test
# LOGIN_PERMISSIONS_test=~*, widgets/database
23 changes: 22 additions & 1 deletion packages/tools/src/filterName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}

Expand Down

0 comments on commit 25ae5bf

Please sign in to comment.