UNPKG

1.57 kBJavaScriptView Raw
1import * as characters from "./characters";
2import { createRootNode, createOrGetChild, NODE_TYPE_ROOT, } from "./nodes";
3export const parseTrie = (serializedTrie) => {
4 const rootNode = createRootNode();
5 let domain = "";
6 let parentNode = rootNode;
7 // Type assertion necessary here due to a TypeScript unsoundness
8 // https://github.com/microsoft/TypeScript/issues/9998#issuecomment-235963457
9 let node = rootNode;
10 const addDomain = () => {
11 node = createOrGetChild(parentNode, domain);
12 domain = "";
13 };
14 for (let i = 0; i < serializedTrie.length; i++) {
15 const char = serializedTrie.charAt(i);
16 switch (char) {
17 case characters.SAME: {
18 addDomain();
19 continue;
20 }
21 case characters.DOWN: {
22 addDomain();
23 parentNode = node;
24 continue;
25 }
26 case characters.RESET: {
27 addDomain();
28 parentNode = rootNode;
29 continue;
30 }
31 case characters.UP: {
32 if (parentNode.type === NODE_TYPE_ROOT) {
33 throw new Error(`Error in serialized trie at position ${i}: Cannot go up, current parent node is already root`);
34 }
35 addDomain();
36 parentNode = parentNode.parent;
37 continue;
38 }
39 }
40 domain += char;
41 }
42 if (domain !== "") {
43 addDomain();
44 }
45 return rootNode;
46};
47//# sourceMappingURL=parse-trie.js.map
\No newline at end of file