diff --git a/lib/util/trie.js b/lib/util/trie.js index b517073..43585ab 100644 --- a/lib/util/trie.js +++ b/lib/util/trie.js @@ -105,7 +105,7 @@ export const insert = trie => key => value => { }; // given a prefix, return a string X such that prefix+X is a possibly larger prefix for the same entries as the original prefix. -export const growKey = trie => key => { +export const growPrefix = trie => key => { const [pos, prefix] = binarySearch(trie.children, key); if (prefix.length === 0) { return ""; @@ -121,7 +121,7 @@ export const growKey = trie => key => { } if (key.length > haveKey.length) { if (key.startsWith(haveKey)) { - return growKey(haveChildNode)(key.slice(haveKey.length)); + return growPrefix(haveChildNode)(key.slice(haveKey.length)); } } return ""; diff --git a/tests/trie.js b/tests/trie.js index 01ca3d5..7016b97 100644 --- a/tests/trie.js +++ b/tests/trie.js @@ -1,5 +1,5 @@ import { pretty } from "../lib/util/pretty.js"; -import { insert, emptyTrie, growKey, suggest } from "../lib/util/trie.js"; +import { insert, emptyTrie, growPrefix, suggest } from "../lib/util/trie.js"; // insertion const with1Item = insert(emptyTrie)('abba')('dancing queen'); @@ -20,11 +20,11 @@ const with8Items = insert(with7Items)('')('hi!'); console.log(pretty(with8Items)); // grow key (for auto-complete) -console.log(growKey(with6Items)("a")); // b -console.log(growKey(with6Items)("ab")); // (empty string) -console.log(growKey(with6Items)("abb")); // a -console.log(growKey(with6Items)("f")); // ood -console.log(growKey(with6Items)("abo")); // riginal +console.log(growPrefix(with6Items)("a")); // b +console.log(growPrefix(with6Items)("ab")); // (empty string) +console.log(growPrefix(with6Items)("abb")); // a +console.log(growPrefix(with6Items)("f")); // ood +console.log(growPrefix(with6Items)("abo")); // riginal // suggest (also for auto-complete) console.log(suggest(with8Items)("a")(3)); // 'ab', 'abba', 'aboriginal'