-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.js
152 lines (139 loc) · 4.09 KB
/
format.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
function transpose(matrix) {
return matrix[0].map((col, i) => matrix.map(row => row[i]));
}
const cumulativeSum = (sum => value => sum += value)(0);
const node = (head, ...children) => ({ head, children });
// const HORIZONTAL = "─";
// const LEFT_DOWN = "┌";
// const MID_DOWN = "┬";
// const RIGHT_DOWN = "┐";
// const BOX_CHARS = [LEFT_DOWN, MID_DOWN, RIGHT_DOWN];
// const LEFT_PADS = [" ", HORIZONTAL, HORIZONTAL];
// const RIGHT_PADS = [HORIZONTAL, HORIZONTAL, " "];
const BOX_CHARS = ["┌", "├", "└"];
const box = (tineIndex, lines) => {
let maxSize = Math.max(...lines.map(e => e.length));
return {
tineIndex,
lines: lines.map(line => line.padEnd(maxSize))
};
};
// returns list of lines
const boxTreeRec = (tree) => {
if(tree.children.length === 0) {
return box(0, ["─ " + tree.head]);
}
let size = tree.head.length;
let prepend = `─ ${tree.head} ─`;
let pad = " ".repeat(prepend.length);
let childBoxes = tree.children.map(child => boxTreeRec(child));
// let middleIndex = Math.floor(childBoxes.length / 2);
// let myTine = childBoxes[middleIndex].tineIndex;
let walkingTine = 0;
// let tines = [];
let lines = [];
let metFirst = false;
let metLast = false;
childBoxes.forEach((b, i) => {
let startTine = walkingTine;
let relativeTine = walkingTine + b.tineIndex;
// tines.push(relativeTine);
b.lines.forEach((line, j) => {
let build = "";
if(walkingTine === relativeTine) {
if(childBoxes.length === 1) {
build += "─";
}
else if(i === 0) {
build += "┌";
metFirst = true;
}
else if(i === childBoxes.length - 1) {
metLast = true;
build += "└";
}
else {
build += "│";
}
}
else {
build += metLast || !metFirst ? " " : "│";
// build += "?";
}
// build += walkingTine.toString(16) + ":" + relativeTine.toString(16);
build += line;
lines.push(build);
walkingTine++;
});
if(walkingTine - startTine > 1 && i + 1 !== childBoxes.length) {
lines.push("│");
walkingTine++;
}
});
myTine = Math.floor(lines.length / 2);
lines = lines.map((line, i) => {
line = (i === myTine ? prepend : pad) + line
return line
.replace(/─│─/g, "─┼─")
.replace(/│─/g, "├─")
.replace(/─┌/g, "─┬")
.replace(/─│/g, "─┤")
.replace(/─└/g, "─┴")
});
// console.log("tines =", tines);
let resultTine = myTine;
let res = box(resultTine, lines);
console.log("resulting box =",res);
return res;
};
const boxTree = (tree) => {
// console.log(tree);
return boxTreeRec(tree).lines.join("\n");
};
let trees = [
node('"', node("+")),
node('"', node("Ψ",
node("left"),
node("mid"),
node("right"),
)),
node("@", node("1354"), node("196")),
node("@",
node("+", node("2"), node("3")),
node("/", node("9"), node("5"))
),
node("@",
node('"',
node("@",
node("1"),
node("@",
node("2"),
node("3")
)
)
),
node("4")
),
// (0<<.)#%(#*#)O"\(#;2&R)@.
node("@.",
node("Ψ",
node("0"),
node("<"),
node("<."),
),
node("/", node('"', node("O",
node("#"),
node("%"),
node("Ψ", node("#"), node("*"), node("#")),
))),
node("Ψ",
node("#"),
node(";"),
node("&", node("2"), node("R")),
)
),
];
for(let tree of trees) {
console.log(boxTree(tree));
console.log("=".repeat(30));
}