-
Notifications
You must be signed in to change notification settings - Fork 6
/
prettytable.js
241 lines (217 loc) · 7.42 KB
/
prettytable.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
240
241
var parse = require('csv-parse/sync');
var fs = require('fs');
var PrettyTable = function () {
// Skeleton structure of table with list of column names, row and max width of each column element
this.table = {
'columnNames': [],
'rows': [],
'maxWidth': []
};
this.version = '0.3.1';
};
// Define list of columns for the table
PrettyTable.prototype.fieldNames = function (names) {
this.table.columnNames = names;
for (var i = 0; i < names.length; i++) {
this.table.maxWidth.push(names[i].length);
}
};
// Add a single row to the table
PrettyTable.prototype.addRow = function (row) {
this.table.rows.push(row);
for (var i = 0; i < row.length; i++) {
if (row[i].toString().length > this.table.maxWidth[i]) {
this.table.maxWidth[i] = row[i].toString().length;
}
}
};
// Single function to create table when headers and array of rows passed
PrettyTable.prototype.create = function (headers, rows) {
// Add table headers
this.fieldNames(headers);
// Add rows one by one
for (var i = 0; i < rows.length; i++) {
this.addRow(rows[i]);
}
};
// Convert the table to string
PrettyTable.prototype.toString = function () {
var finalTable = '';
var columnString = '| ';
var rowString = '';
var lengthDifference = '';
// Draw a line based on the max width of each column and return
var drawLine = function (table) {
var finalLine = '+';
for (var i = 0; i < table.maxWidth.length; i++) {
finalLine += Array(table.maxWidth[i] + 3).join('-') + '+';
}
return finalLine;
};
// If no columns present, return empty string
if (this.table.columnNames.length === 0) {
return finalTable;
}
// Create the table header from column list
for (var i = 0; i < this.table.columnNames.length; i++) {
columnString += this.table.columnNames[i];
// Adjust for max width of the column and pad spaces
if (this.table.columnNames[i].length < this.table.maxWidth[i]) {
lengthDifference = this.table.maxWidth[i] - this.table.columnNames[i].length;
columnString += Array(lengthDifference + 1).join(' ');
}
columnString += ' | ';
}
finalTable += drawLine(this.table) + '\n';
finalTable += columnString + '\n';
finalTable += drawLine(this.table) + '\n';
// Construct the table body
for (i = 0; i < this.table.rows.length; i++) {
var tempRowString = '| ';
for (var k = 0; k < this.table.rows[i].length; k++) {
tempRowString += this.table.rows[i][k];
// Adjust max width of each cell and pad spaces as necessary
if (this.table.rows[i][k].toString().length < this.table.maxWidth[k]) {
lengthDifference = this.table.maxWidth[k] - this.table.rows[i][k].toString().length;
tempRowString += Array(lengthDifference + 1).join(' ');
}
tempRowString += ' | ';
}
rowString += tempRowString + '\n';
}
// Remove newline from the end of the table string
rowString = rowString.slice(0, -1);
// Append to the final table string
finalTable += rowString + '\n';
// Draw last line and return
finalTable += drawLine(this.table) + '\n';
return finalTable;
};
// Write the table string to the console
PrettyTable.prototype.print = function () {
console.log(this.toString());
};
// Write the table string to the console as HTML table formats
PrettyTable.prototype.html = function (attributes) {
// If attributes provided, add them as inline properties, else create default table tag
var htmlTable = '';
if (typeof attributes == 'undefined') {
htmlTable = '<table>';
}
else {
var attributeList = [];
for (var key in attributes) {
attributeList.push(key + '=\'' + attributes[key] + '\'');
}
var attributeString = attributeList.join(' ');
htmlTable = '<table ' + attributeString + '>';
}
// Define the table headers in <thead> from table column list
var tableHead = '<thead><tr>';
for (var i = 0; i < this.table.columnNames.length; i++) {
var headerString = '<th>' + this.table.columnNames[i] + '</th>';
tableHead += headerString;
}
tableHead += '</tr></thead>';
htmlTable += tableHead;
// Construct the table body from the array of rows
var tableBody = '<tbody>';
for (i = 0; i < this.table.rows.length; i++) {
var rowData = '<tr>';
for (var k = 0; k < this.table.rows[i].length; k++) {
var cellData = '<td>' + this.table.rows[i][k] + '</td>';
rowData += cellData;
}
rowData += '</tr>';
tableBody += rowData;
}
// Close all tags and return
tableBody += '</tbody>';
htmlTable += tableBody;
htmlTable += '</table>';
return htmlTable;
};
// Create the table from a CSV file
PrettyTable.prototype.csv = function (filename) {
var csvdata = fs.readFileSync(filename, 'utf8');
var records = parse(csvdata);
var lineCounter = 0;
for (var i = 0; i < records.length; i++) {
if (lineCounter === 0) {
this.fieldNames(records[i]);
lineCounter += 1;
}
else {
this.addRow(records[i]);
lineCounter += 1;
}
}
};
// Create the table from a JSON file
PrettyTable.prototype.json = function (filename) {
var rowKeys = '';
var rowVals = '';
var jsondata = JSON.parse(fs.readFileSync(filename, 'utf8'));
for (var i = 0; i < jsondata.length; i++) {
rowKeys = Object.keys(jsondata[i]);
rowVals = [];
for (var k = 0; k < rowKeys.length; k++) {
rowVals.push(jsondata[i][rowKeys[k]]);
}
if (this.table.columnNames.length === 0) {
this.fieldNames(rowKeys);
}
this.addRow(rowVals);
}
};
// Sort the table given a column in ascending or descending order
PrettyTable.prototype.sortTable = function (colname, reverse) {
// Find the index of the column given the name
var colindex = this.table.columnNames.indexOf(colname);
// Comparator method which takes the column index and sort direction
var Comparator = function ( a, b) {
if (typeof reverse === 'boolean' && reverse === true) {
if (a[colindex] < b[colindex]) {
return 1;
}
else if (a[colindex] > b[colindex]) {
return -1;
}
else {
return 0;
}
}
else {
if (a[colindex] < b[colindex]) {
return -1;
}
else if (a[colindex] > b[colindex]) {
return 1;
}
else {
return 0;
}
}
}
// Sort array of table rows
this.table.rows = this.table.rows.sort(Comparator);
};
// Delete a single row from the table given row number
PrettyTable.prototype.deleteRow = function (rownum) {
if (rownum <= this.table.rows.length && rownum > 0) {
this.table.rows.splice(rownum - 1, 1);
}
};
// Clear the contents from the table, but keep columns and structure
PrettyTable.prototype.clearTable = function () {
this.table.rows = [];
};
// Delete the entire table
PrettyTable.prototype.deleteTable = function () {
this.table = {
'columnNames': [],
'rows': [],
'maxWidth': []
};
};
module.exports = PrettyTable;