-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprint-output.js
More file actions
40 lines (32 loc) · 1.14 KB
/
print-output.js
File metadata and controls
40 lines (32 loc) · 1.14 KB
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
const chalk = require('chalk');
const table = require('text-table');
function csv(headings, rows) {
const string = [headings, ...rows].map(column => column.join(',')).join('\n');
console.log(string);
}
function prettyTable(header, headings, rows) {
const formattedHeadings = headings.map(heading => chalk.cyan(heading));
const formattedRows = rows.map(([name, ...rest]) => [
chalk.blue(name),
...rest.map(cell => chalk.yellow(cell))
]);
const content = table([formattedHeadings, ...formattedRows], {
align: rows[0].map(() => 'r')
});
console.log(`\n${header}\n${content}\n`);
}
function nativeTable(headings, rows) {
if (typeof console.table !== 'function') {
prettyTable('table output is not supported in your environment', headings, rows);
return;
}
const object = rows.reduce((data, [name, ...metrics]) => ({
...data,
[name]: headings.slice(1).reduce((rowData, heading, index) => ({
...rowData,
[heading]: metrics[index]
}), {})
}), {});
console.table(object);
}
module.exports = { csv, prettyTable, nativeTable };