-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
looping.js
59 lines (52 loc) · 1.19 KB
/
looping.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
57
58
59
class Looping {
static parseKey(content, type) {
content = content.trim();
// starting and ending parens are optional
if(content.startsWith("(") && content.endsWith(")")) {
content = content.slice(1, -1);
}
let [first, second, third] = content.split(",").map(entry => entry.trim());
if(type === "Object") {
return {
key: first,
value: second,
index: third,
}
}
return {
value: first,
index: second
};
}
static parse(loopAttr) {
let delimiters = [
{
value: " in ",
type: "Object",
wrap: (content) => {
content = content.trim();
if(content.startsWith("{") && content.endsWith("}")) {
return `(${content})`;
}
return content;
}
},
{
value: " of ",
type: "Array",
}
];
for(let delimiter of delimiters) {
if(loopAttr.includes(delimiter.value)) {
let [keysValue, content] = loopAttr.split(delimiter.value);
return {
keys: Looping.parseKey(keysValue, delimiter.type),
type: delimiter.type,
content: delimiter.wrap ? delimiter.wrap(content) : content,
}
}
}
throw new Error(`Invalid ${AstSerializer.attrs.LOOP} attribute value: ${loopAttr}`);
}
}
export { Looping };