/
parseTrie.js
56 lines (46 loc) · 1.24 KB
/
parseTrie.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"use strict";
const SEPARATORS = require("./separators");
function parseTrie(input) {
let map = new Map();
const parentMaps = [map];
let domain = "";
function setDomain(value) {
if (domain === "") {
return;
}
map.set(domain, value);
domain = "";
}
for (let i = 0; i < input.length; i++) {
const char = input.charAt(i);
switch (char) {
case SEPARATORS.SAME: {
setDomain(true);
continue;
}
case SEPARATORS.DOWN: {
const childMap = new Map();
setDomain(childMap);
parentMaps.push(map);
map = childMap;
continue;
}
case SEPARATORS.RESET: {
setDomain(true);
// Remove all parent maps but the top most
parentMaps.length = 1;
map = parentMaps[0];
continue;
}
case SEPARATORS.UP: {
setDomain(true);
map = parentMaps.pop();
continue;
}
}
domain += char;
}
setDomain(true);
return parentMaps[0];
}
module.exports = parseTrie;