diff --git a/lib/util/trie.js b/lib/util/trie.js index 262ed35..3c70968 100644 --- a/lib/util/trie.js +++ b/lib/util/trie.js @@ -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); } diff --git a/tests/trie.js b/tests/trie.js index 72c6a8f..215c371 100644 --- a/tests/trie.js +++ b/tests/trie.js @@ -99,4 +99,9 @@ assert.equal( assert.deepEqual( suggest(bigTrie)("ooo")(2), [] +); + +assert.deepEqual( + suggest(bigTrie)("df")(2), + [] ); \ No newline at end of file