lyndseybrowning / trie-prefix-tree

Create and modify trie prefix structures and extract word lists including prefixes, anagrams and sub-anagrams
49 stars 14 forks source link

Issues when word contains $ #8

Open KamasamaK opened 5 years ago

KamasamaK commented 5 years ago

If I have something like

var triePrefixTree = require("trie-prefix-tree");

var allFunctionNames = triePrefixTree([]);

allFunctionNames.addWord("test$clone");

console.log(allFunctionNames.getPrefix("te"));

I get ["test", "test$clone"] even though I never added test.

Worse, if I do add test as below

var triePrefixTree = require("trie-prefix-tree");

var allFunctionNames = triePrefixTree([]);

allFunctionNames.addWord("test");
allFunctionNames.addWord("test$clone");

console.log(allFunctionNames.getPrefix("te"));

it produces an error: TypeError: Cannot create property 'c' on number '1'

lyndseybrowning commented 5 years ago

This issue has now been resolved in 1.5.1. If you have any questions, please get in touch.

KamasamaK commented 5 years ago

@lyndseybrowning Thanks! It looks like the my second example does not produce an error anymore and gets the expected result. However, the first example still erroneously shows that test was added to the trie.

I've found that using $ is problematic in a few other situations too:

var triePrefixTree = require("trie-prefix-tree");

var allFunctionNames = triePrefixTree([]);

allFunctionNames.addWord("$test");
allFunctionNames.addWord("$");

console.log(allFunctionNames.getPrefix("te"));

Produces an error TypeError: Cannot create property '$' on number '1'.

var triePrefixTree = require("trie-prefix-tree");

var allFunctionNames = triePrefixTree([]);

allFunctionNames.addWord("$test");
allFunctionNames.addWord("$clone");

console.log(allFunctionNames.getPrefix("$te"));

Returns an empty array.

Should I not use this package if my words contain $ or are these things that can be addressed?

lyndseybrowning commented 5 years ago

The issue is that the Trie uses the $ key to indicate the end of a word. I'd rather not change that.

I think my previous fix can be used to resolve the remaining issues. I'll take a look and get back to you.