-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP feat: add fuzzy search to popup menu and search pad
- Loading branch information
1 parent
99c1ad7
commit 7a7eb7f
Showing
5 changed files
with
144 additions
and
1 deletion.
There are no files selected for viewing
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,120 @@ | ||
import Fuse from 'fuse.js/basic'; | ||
|
||
/** | ||
* @typedef { { | ||
* index: number; | ||
* match: boolean; | ||
* value: string; | ||
* } } Token | ||
* | ||
* @typedef {Token[]} Tokens | ||
*/ | ||
|
||
/** | ||
* @template R | ||
* | ||
* @typedef { { | ||
* item: R, | ||
* tokens: Record<string, Tokens> | ||
* } } SearchResult | ||
*/ | ||
|
||
/** | ||
* Search items using fuzzy search. | ||
* | ||
* @template T | ||
* | ||
* @param {T[]} items | ||
* @param {string} pattern | ||
* @param { { | ||
* keys: string[]; | ||
* } } options | ||
* | ||
* @returns {SearchResult<T>[]} | ||
*/ | ||
export default function fuzzySearch(items, pattern, options) { | ||
const fuse = new Fuse(items, { | ||
includeScore: true, | ||
ignoreLocation: true, | ||
includeMatches: true, | ||
threshold: 0.25, | ||
keys: options.keys | ||
}); | ||
|
||
const result = fuse.search(pattern); | ||
|
||
console.log(result); | ||
|
||
return result.map(({ item, matches }) => { | ||
let tokens = {}; | ||
|
||
for (const key of options.keys) { | ||
|
||
if (item[key] && item[key].length) { | ||
tokens = { | ||
...tokens, | ||
[key]: matchesToTokens(matches, key, item[key]) | ||
}; | ||
} else { | ||
tokens = { | ||
...tokens, | ||
[key]: [] | ||
}; | ||
} | ||
} | ||
|
||
return { | ||
item, | ||
tokens | ||
}; | ||
}); | ||
}; | ||
|
||
function matchesToTokens(matches, key, value) { | ||
const match = matches.find((match) => match.key === key); | ||
|
||
if (!match) { | ||
return [ | ||
{ | ||
index: 0, | ||
value | ||
} | ||
]; | ||
} | ||
|
||
const { indices } = match; | ||
|
||
const tokensMatch = indices.map(([ start, end ]) => { | ||
return { | ||
index: start, | ||
match: true, | ||
value: match.value.slice(start, end + 1) | ||
}; | ||
}); | ||
|
||
const tokens = []; | ||
|
||
let lastIndex = 0; | ||
|
||
tokensMatch.forEach((token, index) => { | ||
if (token.index !== lastIndex) { | ||
tokens.push({ | ||
index: lastIndex, | ||
value: match.value.slice(lastIndex, token.index) | ||
}); | ||
} | ||
|
||
tokens.push(token); | ||
|
||
lastIndex = token.index + token.value.length; | ||
|
||
if (index === tokensMatch.length - 1 && lastIndex !== match.value.length) { | ||
tokens.push({ | ||
index: lastIndex, | ||
value: match.value.slice(lastIndex) | ||
}); | ||
} | ||
}); | ||
|
||
return tokens; | ||
} |
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,5 @@ | ||
import fuzzySearch from './fuzzySearch'; | ||
|
||
export default { | ||
search: [ 'value', fuzzySearch ] | ||
}; |
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