-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable-builder.js
More file actions
84 lines (80 loc) · 2.84 KB
/
table-builder.js
File metadata and controls
84 lines (80 loc) · 2.84 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
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
const chalk = require('chalk');
/**
* Generate a table with headers
* and values based on the given input.
* @param {object} table All data necessary to build the table
* @param {Array} table.headers Object containing the header values
* @param {Array} table.data List of objects to be printed on the table
* @param {?object} options Optional config
* @param {?object} options.spacesBetweenColumns Optional config
* @param {?object} options.headerColor Color of the table header
* @returns {string} Formatted table
*/
function buildTable(table, options) {
const maxOffsetMap = getMaximumOffset(table);
const msg = generateOutputString(table, maxOffsetMap, options);
return msg;
}
/**
* Generate a map containing the number of spaces between columns.
* @param {object} table All data necessary to build the table
* @param {Array} table.headers Object containing the header values
* @param {Array} table.data List of objects to be printed on the table
* @returns {Map} Map containing max value length for each column
*/
function getMaximumOffset(table) {
const {headers, data} = table;
const maxLengthMap = new Map();
for (const header of headers) {
maxLengthMap.set(header.dataKey, header.name.length);
}
for (const header of headers) {
for (const item of data) {
const currentElement = item[header.dataKey];
let valueLength;
if (typeof currentElement === 'number') {
valueLength = String(currentElement).length;
} else {
valueLength = currentElement.length;
}
const maxValueForKey = maxLengthMap.get(header.dataKey) || 0;
if (valueLength > maxValueForKey) {
maxLengthMap.set(header.dataKey, valueLength);
}
}
}
return maxLengthMap;
}
/**
* Generate an string well formated.
* @param {object} table All data necessary to build the table
* @param {Array} table.headers Object containing the header values
* @param {Array} table.data List of objects to be printed on the table
* @param {Map} maxLengthMap A map containing the max length for each object
* @param {?object} options Optional config
* @param {?object} options.spacesBetweenColumns Optional config
* @returns {string} String with the table
*/
function generateOutputString(table, maxLengthMap, options) {
const {headers, data} = table;
const {headerColor, spacesBetweenColumns} = options;
//Generate header
let msg = '';
for (const header of headers) {
msg += header.name.padEnd(
maxLengthMap.get(header.dataKey) + spacesBetweenColumns
);
}
//Generate table lines for each object
msg = `${chalk.hex(headerColor)(msg)}\n`;
for (const item of data) {
for (const header of headers) {
msg += String(item[header.dataKey]).padEnd(
maxLengthMap.get(header.dataKey) + spacesBetweenColumns
);
}
msg += '\n';
}
return msg;
}
module.exports = buildTable;