fix bug in trie

This commit is contained in:
Joeri Exelmans 2025-05-10 23:04:13 +02:00
parent 3b8548e9af
commit 29c7b6af61
2 changed files with 9 additions and 4 deletions

View file

@ -182,12 +182,12 @@ const __suggest = (trie, path, remaining, maxSuggestions) => {
return results; return results;
} }
const [pos, prefix] = binarySearch(trie.children, remaining); const [pos] = binarySearch(trie.children, remaining);
if (prefix.length === 0) {
return []; // nothing in common
}
if (pos < trie.children.length) { if (pos < trie.children.length) {
const [haveKey, haveChildNode] = trie.children[pos]; const [haveKey, haveChildNode] = trie.children[pos];
if (!remaining.startsWith(haveKey) && !haveKey.startsWith(remaining)) {
return [];
}
return __suggest(haveChildNode, path+haveKey, remaining.slice(haveKey.length), maxSuggestions); return __suggest(haveChildNode, path+haveKey, remaining.slice(haveKey.length), maxSuggestions);
} }

View file

@ -100,3 +100,8 @@ assert.deepEqual(
suggest(bigTrie)("ooo")(2), suggest(bigTrie)("ooo")(2),
[] []
); );
assert.deepEqual(
suggest(bigTrie)("df")(2),
[]
);