diff --git a/lib/util/trie.js b/lib/util/trie.js index b156e96..328e70b 100644 --- a/lib/util/trie.js +++ b/lib/util/trie.js @@ -19,7 +19,7 @@ export const isProperlySorted = trie => { } } return true; -} +}; // find maximal common prefix, and whether string A is smaller than B const commonPrefix = (strA, strB) => { @@ -34,12 +34,12 @@ const commonPrefix = (strA, strB) => { } } return [strA.slice(0, i), false]; -} +}; // a funny kind of binary search, that assumes 'ls' is a sorted list of strings, and none of the strings have a common prefix const binarySearch = (ls, key) => { return __binarySearch(ls, key, 0, ls.length); -} +}; const __binarySearch = (ls, key, min, max) => { if (min === max) { return [max, ""]; // otherwise we go out of bounds @@ -54,7 +54,7 @@ const __binarySearch = (ls, key, min, max) => { return __binarySearch(ls, key, min, middle); } return __binarySearch(ls, key, middle+1, max); -} +}; const check = trie => { // uncomment if you think shit is broken: @@ -62,7 +62,7 @@ const check = trie => { // throw new Error('not properly sorted!') // } return trie; -} +}; // insert (key,value) into trie. export const insert = trie => key => value => { @@ -118,15 +118,20 @@ export const insert = trie => key => value => { children: trie.children.with( insertPos, // position to update [prefix, { + value: (postFix.length === 0) ? value : undefined, children: (havePostFix < postFix) ? [ [havePostFix, haveChildNode], [postFix, {value, children: []}], ] - : [ - [postFix, {value, children: []}], - [havePostFix, haveChildNode], - ], + : ((postFix.length > 0) + ? [ + [postFix, {value, children: []}], + [havePostFix, haveChildNode], + ] + : [ + [havePostFix, haveChildNode], + ]), }], ), }); @@ -153,12 +158,12 @@ export const growPrefix = trie => key => { } } return ""; -} +}; // get array of (key, value) entries whose keys are prefixed by 'key'. export const suggest = trie => key => maxSuggestions => { return __suggest(trie, "", key, maxSuggestions); -} +}; export const get = trie => key => { if (key === '') { @@ -171,7 +176,7 @@ export const get = trie => key => { return get(haveChildNode)(key.slice(haveKey.length)); } } -} +}; const __suggest = (trie, path, remaining, maxSuggestions) => { if (maxSuggestions === 0) { @@ -205,4 +210,4 @@ const __suggest = (trie, path, remaining, maxSuggestions) => { } return []; -} +}; diff --git a/tests/trie.js b/tests/trie.js index 116158b..a61b40e 100644 --- a/tests/trie.js +++ b/tests/trie.js @@ -27,6 +27,8 @@ const with7Items = insert(with6Items)('ab')('yup'); console.log(pretty(with7Items)); const with8Items = insert(with7Items)('')('hi!'); console.log(pretty(with8Items)); +const with9Items = insert(with8Items)('fo')(42); +console.log(pretty(with9Items)); /////////////////// // Setup TRIE 2 ... @@ -82,11 +84,22 @@ assert.deepEqual( ]); assert.equal( - isProperlySorted(with8Items), + isProperlySorted(with9Items), true, "trie should always be properly sorted!" ); +assert.equal( + get(with9Items)(''), + "hi!" +) + + +assert.equal( + get(with9Items)('fo'), + 42 +) + assert.equal( isProperlySorted(bigTrie), true,