trie: add get function

This commit is contained in:
Joeri Exelmans 2025-05-11 09:30:48 +02:00
parent dd35e2b577
commit dadee965e5
2 changed files with 26 additions and 1 deletions

View file

@ -160,6 +160,19 @@ export const suggest = trie => key => maxSuggestions => {
return __suggest(trie, "", key, maxSuggestions);
}
export const get = trie => key => {
if (key === '') {
return trie.value;
}
const [pos] = binarySearch(trie.children, key);
if (pos < trie.children.length) {
const [haveKey, haveChildNode] = trie.children[pos];
if (key.startsWith(haveKey)) {
return get(haveChildNode)(key.slice(haveKey.length));
}
}
}
const __suggest = (trie, path, remaining, maxSuggestions) => {
if (maxSuggestions === 0) {
return [];