-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
33 lines (27 loc) · 896 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
/* eslint-disable function-paren-newline */
/* eslint-disable implicit-arrow-linebreak */
function drawTable(data) {
const headers = Object.keys(data[0]);
const columnWidths = headers.map((header) =>
Math.max(header.length, ...data.map((row) => `${row[header]}`.length)),
);
const separator = `+${columnWidths
.map((width) => '-'.repeat(width + 2))
.join('+')}+`;
const headerRowFormatted = `| ${headers
.map((header, i) => {
const headerFormatted = header.charAt(0).toUpperCase() + header.slice(1);
return headerFormatted.padEnd(columnWidths[i]);
})
.join(' | ')} |`;
const rows = data.map(
(row) =>
`| ${headers
.map((key, i) => `${row[key]}`.padEnd(columnWidths[i]))
.join(' | ')} |`,
);
return [separator, headerRowFormatted, separator, ...rows, separator].join(
'\n',
);
}
module.exports = drawTable;