fix Trie.suggest (for real this time?)

This commit is contained in:
Joeri Exelmans 2025-05-10 19:51:00 +02:00
parent 660512cc19
commit 075cc1244f

View file

@ -136,7 +136,8 @@ const __suggest = (trie, path, remaining, maxSuggestions) => {
if (maxSuggestions === 0) { if (maxSuggestions === 0) {
return []; return [];
} }
if (remaining === "") {
if (remaining === '') {
const results = []; const results = [];
if (trie.value !== undefined) { if (trie.value !== undefined) {
results.push([path, trie.value]); results.push([path, trie.value]);
@ -152,9 +153,12 @@ const __suggest = (trie, path, remaining, maxSuggestions) => {
} }
return results; return results;
} }
const [pos, prefix] = binarySearch(trie.children, remaining); const [pos, prefix] = binarySearch(trie.children, remaining);
if (prefix.length === haveKey.length) { if (pos < trie.children.length) {
return __suggest(haveChildNode, path+haveKey, remaining.slice(haveKey.length), maxSuggestions) const [haveKey, haveChildNode] = trie.children[pos];
return __suggest(haveChildNode, path+haveKey, remaining.slice(haveKey.length), maxSuggestions);
} }
return []; return [];
} }