-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (37 loc) · 917 Bytes
/
index.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
import chalk from "chalk";
let chanks = {
"more": "├",
"gap": "│ ",
"last": "└",
"label": "─ "
};
export function CAT (ast, options) {
let isRoot = !options;
if (!options) {
options = {};
}
if (!options.prefix) {
options.prefix = [];
}
let color = ast.color ? chalk[ast.color] : null, row = [];
if (color instanceof Function) {
ast.label = color(ast.label || "");
}
ast.label = options.prefix.join("") + (chanks[options.chank] ? chanks[options.chank] + chanks["label"] : "") + ast.label;
row.push(ast.label);
if (ast.nodes) {
if (!isRoot) {
if (options.chank === "last") {
options.prefix.push(" ");
} else {
options.prefix.push(chanks["gap"]);
}
}
for (let i = 0; i < ast.nodes.length; i++) {
options.chank = i === ast.nodes.length - 1 ? "last" : "more";
row.push(CAT(ast.nodes[i], options));
options.prefix.pop();
}
}
return row.join("\n");
}