-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.js
44 lines (36 loc) · 971 Bytes
/
solution.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
function drawTable(data) {
if (!data || !data.length) return "";
let columns = [];
data.forEach((row, i) => {
if (columns.length === 0) {
columns = Object.keys(row).map((c) => [
c.charAt(0).toUpperCase() + c.slice(1),
c.length,
]);
}
let j = 0;
for (const [key, value] of Object.entries(row)) {
columns[j][1] = Math.max(String(value).length, columns[j][1]);
j++;
}
});
const divider =
"+" + columns.map((col) => "-".repeat(col[1] + 2) + "+").join("");
const table = [];
table.push(divider);
const header =
"|" +
columns.map(([col, width]) => ` ${String(col).padEnd(width)} |`).join("");
table.push(header);
table.push(divider);
data.forEach((line) => {
const row =
"|" +
Object.values(line)
.map((v, i) => ` ${String(v).padEnd(columns[i][1])} |`)
.join("");
table.push(row);
});
table.push(divider);
return table.join("\n");
}