-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextParser.js
More file actions
106 lines (103 loc) · 4.11 KB
/
Copy pathTextParser.js
File metadata and controls
106 lines (103 loc) · 4.11 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
TextParser = {};
TextParser.parseCustomFormat = function (text) {
var content = text.replace(/\n\s*/g,'').split(':'); // Removes newlines \n and leading spaces \s* after newlines.
var commands = {};
for (var i = 0, depth = 0, levels = []; i < content.length; i++) {
var arg = content[i];
if (!arg) { // "" means consecutive. Go down one level.
depth++;
continue;
}
if (!levels[depth-1]) depth = 0; // If we go down but there were no levels above use... then we must be at the top.
if (!depth) { // Make New Property
addLevel(commands, arg, true);
levels = [arg];
continue;
}
// TRAVERSING THE DEPTHS!!!
var currentLevel = commands;
for (var d = 0; d < depth-1; d++) {
if (typeof currentLevel[levels[d]] !== 'object') { // parent[level] = value
var val = currentLevel[levels[d]];
currentLevel[levels[d]] = {};
currentLevel[levels[d]][val] = true;
currentLevel = currentLevel[levels[d]];
continue;
}
if (currentLevel[levels[d]].length) { // parent[level] = []
if (typeof currentLevel[levels[d]][currentLevel[levels[d]].length-1] !== 'object') { // parent[level][N] = value
var val = currentLevel[levels[d]][currentLevel[levels[d]].length-1];
currentLevel[levels[d]][currentLevel[levels[d]].length-1] = {};
currentLevel[levels[d]][currentLevel[levels[d]].length-1][val] = true;
currentLevel = currentLevel[levels[d]][currentLevel[levels[d]].length-1];
} else {
currentLevel = currentLevel[levels[d]][currentLevel[levels[d]].length-1];
}
} else {
currentLevel = currentLevel[levels[d]];
}
}
// parent = true | make parent = child
// parent = child | make parent = {child:[true, true]}
// parent = value | make parent = {value: true,child: true}
if (typeof currentLevel[levels[depth-1]] !== 'object') { // parent = someVal
if (currentLevel[levels[depth-1]] === true) { // parent = true
currentLevel[levels[depth-1]] = arg;
} else { // parent = value
var val = currentLevel[levels[depth-1]];
currentLevel[levels[depth-1]] = {};
currentLevel[levels[depth-1]][val] = true;
if (currentLevel[levels[depth-1]][arg]) currentLevel[levels[depth-1]][arg] = [val, true];
else {
currentLevel[levels[depth-1]][val] = true;
currentLevel[levels[depth-1]][arg] = true;
}
}
}
else if (currentLevel[levels[depth-1]].length) { // parent = []
var n = currentLevel[levels[depth-1]].length-1
if (typeof currentLevel[levels[depth-1]][n] !== 'object') { // parent[n] = true or parent[n] = value
if (currentLevel[levels[depth-1]][n] === true) { // parent[n]
currentLevel[levels[depth-1]][n] = arg;
} else { // parent[n] = value
var val = currentLevel[levels[depth-1]][n];
currentLevel[levels[depth-1]][n] = {};
addLevel(currentLevel[levels[depth-1]][n], val);
addLevel(currentLevel[levels[depth-1]][n], arg);
}
} else {
addLevel(currentLevel[levels[depth-1]][n], arg);
}
} else {
addLevel(currentLevel[levels[depth-1]], arg);
}
levels[depth] = arg;
depth = 0;
}
return commands; // Return the object built from the parsed text
// Define addLevel() function
// Eases the headache of rewriting this code a lot.
function addLevel(parent, child, topLevel) {
// topLevel is a bool to indicate whether our parent is the `commands` object.
if (topLevel) {
if (!parent[child]) parent[child] = true; // child: undefined
else if (typeof parent[child] === 'object' && parent[child].length) parent[child].push(true); // child: []
else parent[child] = [parent[child], true]; // child: value
} else {
// parent = [...] | make parent = [...,child]
// parent = {} | make parent[child] = true
// parent = {child:true} | make parent[child] = [true, true]
if (!parent[child]) { // parent = {...} | make parent[child] = true
parent[child] = true;
} else if (typeof parent[child] !== 'object') { // parent[child] = ... | make parent[child] = [..., true]
var val = parent[child];
parent[child] = {};
parent[child] = [val, true];
} else if (parent[child].length) {
parent[child].push(true);
} else {
parent[child] = [parent[child], true];
}
}
};
}