-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
239 lines (212 loc) · 6.18 KB
/
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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
"use strict";
const VERTICAL = "│";
const HORIZONTAL = "─";
const TOP_LEFT = "┌";
const TOP_RIGHT = "┐";
const BOTTOM_LEFT = "└";
const BOTTOM_RIGHT = "┘";
function repeatStr(chr, length, delimiter = "") {
return Array.from({ length }, () => chr).join(delimiter);
}
function emptyStrings(length) {
return Array.from({ length }, () => "");
}
function padCenter(str, length) {
const leftOver = length - str.length;
const startPad = Math.floor(leftOver / 2);
const endPad = startPad + leftOver % 2;
return str
.padStart(str.length + startPad)
.padEnd(str.length + endPad + startPad);
}
function boxedString(str, columnWidth, rowHeight) {
const vertLength = columnWidth + 2;
const vertLine = repeatStr(HORIZONTAL, vertLength);
if (str.length === 0 || str === ".") {
const whitespace = " ".padEnd(vertLength + 2);
return repeatStr(whitespace, rowHeight, "\n");
}
const paddedString = padCenter(str, columnWidth);
return `${TOP_LEFT}${vertLine}${TOP_RIGHT}\n${VERTICAL} ${paddedString} ${VERTICAL}\n${BOTTOM_LEFT}${vertLine}${BOTTOM_RIGHT}`;
}
function smallConnectedBox(boxRows, rowIndex, cellIndex, columnWidth) {
const padValue = columnWidth + 4;
if (boxRows[rowIndex - 1] && !isBoxEmpty(boxRows[rowIndex - 1][cellIndex])) {
return padCenter(VERTICAL, padValue);
} else {
return padCenter(" ", padValue);
}
}
function jointPoint(hasTop, hasBottom, hasLeft, hasRight) {
if (hasTop && hasBottom && hasLeft && hasRight) {
return "┼";
}
if (hasTop && hasBottom && hasLeft) {
return "┤";
}
if (hasTop && hasBottom && hasRight) {
return "├";
}
if (hasBottom && hasRight && hasLeft) {
return "┬";
}
if (hasTop && hasRight && hasLeft) {
return "┴";
}
if (hasRight && hasBottom) {
return TOP_LEFT;
}
if (hasLeft && hasBottom) {
return TOP_RIGHT;
}
if (hasTop && hasRight) {
return BOTTOM_LEFT;
}
if (hasLeft && hasTop) {
return BOTTOM_RIGHT;
}
if (hasLeft && hasRight) {
return HORIZONTAL;
}
if (hasTop && hasBottom) {
return VERTICAL;
}
return "x";
}
function connectedBox(boxRows, rowIndex, cellIndex, columnWidth, rowHeight) {
if (rowHeight === 1) {
return smallConnectedBox(boxRows, rowIndex, cellIndex, columnWidth);
}
const padValue = columnWidth + 4;
const leftOver = (padValue - 1) % 2;
let result = "";
const hasTop =
boxRows[rowIndex - 1] && !isBoxEmpty(boxRows[rowIndex - 1][cellIndex]);
const hasLeft =
boxRows[rowIndex][cellIndex - 1] &&
!isBoxEmpty(boxRows[rowIndex][cellIndex - 1]);
const hasRight =
boxRows[rowIndex][cellIndex + 1] &&
!isBoxEmpty(boxRows[rowIndex][cellIndex + 1]);
const hasBottom =
boxRows[rowIndex + 1] && !isBoxEmpty(boxRows[rowIndex + 1][cellIndex]);
if (hasTop) {
result += padCenter(VERTICAL, padValue);
} else {
result += padCenter(" ", padValue);
}
if (hasLeft) {
result += `\n${repeatStr(HORIZONTAL, Math.floor((padValue - 1) / 2))}`;
} else {
result += `\n${repeatStr(" ", Math.floor((padValue - 1) / 2))}`;
}
result += jointPoint(hasTop, hasBottom, hasLeft, hasRight);
if (hasRight) {
result += `${repeatStr(
HORIZONTAL,
Math.floor((padValue - 1) / 2) + leftOver
)}`;
} else {
result += `${repeatStr(" ", Math.floor((padValue - 1) / 2) + leftOver)}`;
}
if (hasBottom) {
result += `\n${padCenter(VERTICAL, padValue)}`;
} else {
result += `\n${padCenter(" ", padValue)}`;
}
return result;
}
function isBoxEmpty(box) {
return Array.from(box).filter(s => s !== " " && s !== "\n").length === 0;
}
function joinedBoxes(boxes, rowHeight) {
if (rowHeight === 1) {
return boxes.join(" ");
}
const rows = boxes.reduce((acc, box, index) => {
const lines = box.split("\n");
if (index === 0) {
return [lines[0], lines[1], lines[2]];
}
const line =
isBoxEmpty(box) || isBoxEmpty(boxes[index - 1])
? " "
: repeatStr(HORIZONTAL, 3);
return [
acc[0] + " " + lines[0],
acc[1] + line + lines[1],
acc[2] + " " + lines[2]
];
}, []);
return rows.join("\n");
}
function getColumnWidths(boxes) {
return boxes
.reduce((acc, curr) =>
curr.map(
(cell, index) => (cell.length > acc[index].length ? cell : acc[index])
)
)
.map(s => s.length);
}
function getRowHeights(boxes) {
return boxes.map(row => {
const rowEmpty =
row.filter(cell => cell.length !== 0 && cell !== ".").length === 0;
return rowEmpty ? 1 : 3;
});
}
function getMaxColumnCount(boxRows) {
return boxRows.reduce((max, row) => Math.max(max, row.length), 0);
}
function normalizeInput(str) {
const rows = str.split("\n").map(s => s.split(","));
const maxColumnCount = getMaxColumnCount(rows);
return rows.map(
row =>
row.length < maxColumnCount
? row.concat(emptyStrings(maxColumnCount - row.length))
: row
);
}
function createChart(str) {
const boxRows = normalizeInput(str);
const columnWidths = getColumnWidths(boxRows);
const rowHeights = getRowHeights(boxRows);
const decoratedBoxes = boxRows.map((row, rowIndex) => {
return row.map((cell, cellIndex) => {
if (cell === ".") {
return connectedBox(
boxRows,
rowIndex,
cellIndex,
columnWidths[cellIndex],
rowHeights[rowIndex]
);
} else {
return boxedString(cell, columnWidths[cellIndex], rowHeights[rowIndex]);
}
});
});
const res = decoratedBoxes.map((boxes, index) => {
let connectRow = "";
if (decoratedBoxes[index - 1] && rowHeights[index - 1] !== 1) {
boxes.forEach((box, cellIndex) => {
const isLast = cellIndex === boxes.length - 1;
const upperCell = decoratedBoxes[index - 1][cellIndex];
if (isBoxEmpty(upperCell) || isBoxEmpty(box)) {
connectRow += "".padStart(columnWidths[cellIndex] + (isLast ? 4 : 7));
return;
}
connectRow += padCenter(VERTICAL, columnWidths[cellIndex] + 4);
if (!isLast) {
connectRow += " ";
}
});
connectRow += "\n";
}
return connectRow + joinedBoxes(boxes, rowHeights[index]);
});
return res.join("\n");
}
module.exports = { createChart };