-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
393d82b
commit ab4090d
Showing
5 changed files
with
97 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import FuzzySearch from "@leeoniya/ufuzzy"; | ||
|
||
const uf = new FuzzySearch(); | ||
const infoThresh = 1e3; | ||
|
||
export function search(haystack: string[], needle: string) { | ||
// pre-filter | ||
const idxs = uf.filter(haystack, needle); | ||
|
||
// idxs can be null when the needle is non-searchable (has no alpha-numeric chars) | ||
if (idxs !== null && idxs.length > 0) { | ||
// sort/rank only when <= 1,000 items | ||
if (idxs.length <= infoThresh) { | ||
const info = uf.info(idxs, haystack, needle); | ||
// order is a double-indirection array (a re-order of the passed-in idxs) | ||
// this allows corresponding info to be grabbed directly by idx, if needed | ||
const order = uf.sort(info, haystack, needle); | ||
// render post-filtered & ordered matches | ||
return order.map((_o, i) => haystack[info.idx[order[i]]]); | ||
} else { | ||
// render pre-filtered but unordered matches | ||
return idxs.map((_o, i) => haystack[idxs[i]]); | ||
} | ||
} | ||
|
||
return []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters