diff --git a/lib/util/trie.js b/lib/util/trie.js index 433c630..262ed35 100644 --- a/lib/util/trie.js +++ b/lib/util/trie.js @@ -183,6 +183,9 @@ const __suggest = (trie, path, remaining, maxSuggestions) => { } const [pos, prefix] = binarySearch(trie.children, remaining); + if (prefix.length === 0) { + return []; // nothing in common + } if (pos < trie.children.length) { const [haveKey, haveChildNode] = trie.children[pos]; return __suggest(haveChildNode, path+haveKey, remaining.slice(haveKey.length), maxSuggestions); diff --git a/tests/trie.js b/tests/trie.js index 2c6eaf8..72c6a8f 100644 --- a/tests/trie.js +++ b/tests/trie.js @@ -90,3 +90,13 @@ assert.equal( true, "trie should always be properly sorted!" ); + +assert.equal( + growPrefix(bigTrie)("dict.le"), + "ngth" +); + +assert.deepEqual( + suggest(bigTrie)("ooo")(2), + [] +); \ No newline at end of file