basic-trie
A tiny TypeScript trie implementation with limited wildcards support.
Installation
yarn add basic-trie
Usage
import Trie from 'basic-trie';
const trie = new Trie<number, string>();
trie.get([1, 2, 3]);
trie.set([1, 2, 3], '123');
trie.get([1, 2, 3]);
trie.get([1, 2]);
trie.get([1, 2, 3, 4]);
trie.get([1]);
trie.get([2, 1]);
Wildcards support
Wildcards are supported by overriding a few methods of Trie
class, example:
import Trie, { PayloadType } from 'basic-trie';
class MyTrie extends Trie<string, string> {
isKeyWildcard(key: string): boolean {
return key.startsWith(':');
}
getWildcardPayload(
key: string,
wildcardName: string,
_wildcardValue: string | null,
): PayloadType | undefined {
return {
[wildcardName.substr(1)]: key,
};
}
}
function splitPathString(s: string): string[] {
return s.split('/');
}
function addMyPath(trie: MyTrie, s: string) {
trie.set(splitPathString(s), s);
}
const trie = new MyTrie();
addMyPath(trie, ':a');
addMyPath(trie, ':b');
addMyPath(trie, ':c/:sub1');
addMyPath(trie, 'imp/:sub2');
trie.getWithPayload(['a']);
trie.getWithPayload(['a', 'b', 'c']);
trie.getWithPayload(['a', 'b']);
trie.getWithPayload(['imp', 'b']);