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

View file

@ -99,4 +99,9 @@ assert.equal(
assert.deepEqual(
suggest(bigTrie)("ooo")(2),
[]
);
assert.deepEqual(
suggest(bigTrie)("df")(2),
[]
);