Skip to content

Commit 9df9ed2

Browse files
committed
prettytable methods accessed through object
1 parent e52f2e7 commit 9df9ed2

3 files changed

Lines changed: 138 additions & 165 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
"author": "Jyotiska NK <jyotiska123@gmail.com>",
88
"license": "MIT",
99
"dependencies": {
10-
"fast-csv": "1.0.0"
10+
"csv-parse": "latest"
1111
}
1212
}

prettytable.js

Lines changed: 118 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -1,173 +1,86 @@
1-
var csv = require('fast-csv');
1+
var parse = require('csv-parse/lib/sync');
22
var fs = require('fs');
33

4-
// Skeleton structure of table with list of column names, row and max width of each column element
5-
var table = {
6-
"columnNames": [],
7-
"rows": [],
8-
"maxWidth": []
9-
};
10-
11-
// Single function to create table when headers and array of rows passed
12-
exports.create = function(headers, rows) {
13-
// Add table headers
14-
addTableHeader(headers);
15-
16-
// Add rows one by one
17-
for (var i=0; i < rows.length; i++) {
18-
addTableRow(rows[i]);
19-
}
4+
var PrettyTable = function() {
5+
// Skeleton structure of table with list of column names, row and max width of each column element
6+
this.table = {
7+
"columnNames": [],
8+
"rows": [],
9+
"maxWidth": []
10+
};
2011
};
2112

2213
// Define list of columns for the table
23-
exports.fieldNames = function(names) {
24-
addTableHeader(names);
14+
PrettyTable.prototype.fieldNames = function(names) {
15+
this.table.columnNames = names;
16+
for (var i=0; i < names.length; i++) {
17+
this.table.maxWidth.push(names[i].length);
18+
}
2519
};
2620

2721
// Add a single row to the table
28-
exports.addRow = function(row) {
29-
addTableRow(row);
30-
};
31-
32-
// Convert the table to string
33-
exports.toString = function() {
34-
return tableToString();
35-
};
36-
37-
// Write the table string to the console
38-
exports.print = function() {
39-
console.log(tableToString());
40-
};
41-
42-
// Write the table string to the console as HTML table formats
43-
exports.html = function(attributes) {
44-
return tableToHTML(attributes);
45-
};
46-
47-
// Create the table from a CSV file
48-
exports.csv = function(filename) {
49-
var stream = fs.createReadStream(filename);
50-
line_counter = 0;
51-
var csvStream = csv()
52-
.on("data", function(data){
53-
if (line_counter === 0) {
54-
addTableHeader(data);
55-
line_counter += 1;
56-
} else {
57-
addTableRow(data);
58-
line_counter += 1;
59-
}
60-
})
61-
.on("end", function() {
62-
console.log(tableToString());
63-
});
64-
stream.pipe(csvStream);
65-
};
66-
67-
// Create the table from a JSON file
68-
exports.json = function(filename) {
69-
var jsondata = JSON.parse(fs.readFileSync(filename, 'utf8'));
70-
for (var i=0; i < jsondata.length; i++) {
71-
rowKeys = Object.keys(jsondata[i]);
72-
rowVals = [];
73-
for (var k=0; k < rowKeys.length; k++) {
74-
rowVals.push(jsondata[i][rowKeys[k]]);
75-
}
76-
if (table.columnNames.length === 0) {
77-
addTableHeader(rowKeys);
22+
PrettyTable.prototype.addRow = function(row) {
23+
this.table.rows.push(row);
24+
for (var i=0; i < row.length; i++) {
25+
if (row[i].toString().length > this.table.maxWidth[i]) {
26+
this.table.maxWidth[i] = row[i].toString().length;
7827
}
79-
addTableRow(rowVals);
8028
}
81-
console.log(tableToString());
8229
};
8330

84-
// Sort the table given a column in ascending or descending order
85-
exports.sortTable = function(colname, reverse) {
86-
var reverseSort = false;
87-
if (typeof(reverse) === "boolean" && reverse === true) {
88-
reverseSort = true;
89-
}
90-
sortTableByColumn(colname, reverseSort);
91-
};
92-
93-
// Delete a single row from the table given row number
94-
exports.deleteRow = function(rownum) {
95-
if (rownum <= table.rows.length && rownum > 0) {
96-
table.rows.splice(rownum-1, 1);
97-
}
98-
};
99-
100-
// Clear the contents from the table, but keep columns and structure
101-
exports.clearTable = function() {
102-
table.rows = [];
103-
};
104-
105-
// Delete the entire table
106-
exports.deleteTable = function() {
107-
table = {"columnNames": [], "rows": [], "maxWidth": []};
108-
};
109-
110-
// Draw a line based on the max width of each column and return
111-
var drawLine = function() {
112-
arrayLength = 0;
113-
for (var i=0; i < table.maxWidth.length; i++) {
114-
arrayLength += table.maxWidth[i];
115-
}
116-
return '+' + Array(arrayLength + table.maxWidth.length * 3).join('-') + '+';
117-
};
31+
// Single function to create table when headers and array of rows passed
32+
PrettyTable.prototype.create = function(headers, rows) {
33+
// Add table headers
34+
this.fieldNames(headers);
11835

119-
// Helper method to add column names of the table and add maxwidth for each column element
120-
var addTableHeader = function(names) {
121-
table.columnNames = names;
122-
for (var i=0; i < names.length; i++) {
123-
table.maxWidth.push(names[i].length);
36+
// Add rows one by one
37+
for (var i=0; i < rows.length; i++) {
38+
this.addRow(rows[i]);
12439
}
12540
};
12641

127-
// Helper method to add a row to the table and re-adjust max width of each row element
128-
var addTableRow = function(row) {
129-
table.rows.push(row);
130-
for (var i=0; i < row.length; i++) {
131-
if (row[i].toString().length > table.maxWidth[i]) {
132-
table.maxWidth[i] = row[i].toString().length;
42+
// Convert the table to string
43+
PrettyTable.prototype.toString = function() {
44+
// Draw a line based on the max width of each column and return
45+
var drawLine = function(table) {
46+
arrayLength = 0;
47+
for (var i=0; i < table.maxWidth.length; i++) {
48+
arrayLength += table.maxWidth[i];
13349
}
134-
}
135-
};
50+
return '+' + Array(arrayLength + table.maxWidth.length * 3).join('-') + '+';
51+
};
13652

137-
// Convert the table to string and return
138-
var tableToString = function() {
139-
// Define final table string as empty string
14053
finalTable = "";
14154

14255
// If no columns present, return empty string
143-
if (table.columnNames.length === 0) {
56+
if (this.table.columnNames.length === 0) {
14457
return finalTable;
14558
}
14659

14760
// Create the table header from column list
14861
columnString = "| ";
14962
rowString = "";
150-
for (var i=0; i < table.columnNames.length; i++) {
151-
columnString += table.columnNames[i];
63+
for (var i=0; i < this.table.columnNames.length; i++) {
64+
columnString += this.table.columnNames[i];
15265
// Adjust for max width of the column and pad spaces
153-
if (table.columnNames[i].length < table.maxWidth[i]) {
154-
lengthDifference = table.maxWidth[i] - table.columnNames[i].length;
66+
if (this.table.columnNames[i].length < this.table.maxWidth[i]) {
67+
lengthDifference = this.table.maxWidth[i] - this.table.columnNames[i].length;
15568
columnString += Array(lengthDifference + 1).join(' ');
15669
}
15770
columnString += " | ";
15871
}
159-
finalTable += drawLine() + "\n";
72+
finalTable += drawLine(this.table) + "\n";
16073
finalTable += columnString + "\n";
161-
finalTable += drawLine() + "\n";
74+
finalTable += drawLine(this.table) + "\n";
16275

16376
// Construct the table body
164-
for (i=0; i < table.rows.length; i++) {
77+
for (i=0; i < this.table.rows.length; i++) {
16578
var tempRowString = "| ";
166-
for (var k=0; k < table.rows[i].length; k++) {
167-
tempRowString += table.rows[i][k];
79+
for (var k=0; k < this.table.rows[i].length; k++) {
80+
tempRowString += this.table.rows[i][k];
16881
// Adjust max width of each cell and pad spaces as necessary
169-
if (table.rows[i][k].toString().length < table.maxWidth[k]) {
170-
lengthDifference = table.maxWidth[k] - table.rows[i][k].toString().length;
82+
if (this.table.rows[i][k].toString().length < this.table.maxWidth[k]) {
83+
lengthDifference = this.table.maxWidth[k] - this.table.rows[i][k].toString().length;
17184
tempRowString += Array(lengthDifference + 1).join(' ');
17285
}
17386
tempRowString += " | ";
@@ -179,12 +92,17 @@ var tableToString = function() {
17992
// Append to the final table string
18093
finalTable += rowString + "\n";
18194
// Draw last line and return
182-
finalTable += drawLine() + "\n";
95+
finalTable += drawLine(this.table) + "\n";
18396
return finalTable;
18497
};
18598

186-
// Convert the table to HTML table
187-
var tableToHTML = function(attributes) {
99+
// Write the table string to the console
100+
PrettyTable.prototype.print = function() {
101+
console.log(this.toString());
102+
};
103+
104+
// Write the table string to the console as HTML table formats
105+
PrettyTable.prototype.html = function(attributes) {
188106
// If attributes provided, add them as inline properties, else create default table tag
189107
var htmlTable = "";
190108
if (typeof attributes == "undefined") {
@@ -200,19 +118,19 @@ var tableToHTML = function(attributes) {
200118

201119
// Define the table headers in <thead> from table column list
202120
var tableHead = "<thead><tr>";
203-
for (var i=0; i < table.columnNames.length; i++) {
204-
var headerString = "<th>" + table.columnNames[i] + "</th>";
121+
for (var i=0; i < this.table.columnNames.length; i++) {
122+
var headerString = "<th>" + this.table.columnNames[i] + "</th>";
205123
tableHead += headerString;
206124
}
207125
tableHead += "</tr></thead>";
208126
htmlTable += tableHead;
209127

210128
// Construct the table body from the array of rows
211129
var tableBody = "<tbody>";
212-
for (i=0; i < table.rows.length; i++) {
130+
for (i=0; i < this.table.rows.length; i++) {
213131
var rowData = "<tr>";
214-
for (var k=0; k < table.rows[i].length; k++) {
215-
var cellData = "<td>" + table.rows[i][k] + "</td>";
132+
for (var k=0; k < this.table.rows[i].length; k++) {
133+
var cellData = "<td>" + this.table.rows[i][k] + "</td>";
216134
rowData += cellData;
217135
}
218136
rowData += "</tr>";
@@ -226,14 +144,48 @@ var tableToHTML = function(attributes) {
226144
return htmlTable;
227145
};
228146

229-
// Helper method to sort table given column name
230-
var sortTableByColumn = function(colname, reverse) {
147+
// Create the table from a CSV file
148+
PrettyTable.prototype.csv = function(filename) {
149+
var csvdata = fs.readFileSync(filename, 'utf8');
150+
var records = parse(csvdata);
151+
152+
var lineCounter = 0;
153+
for (var i=0; i < records.length; i++) {
154+
if (lineCounter === 0) {
155+
this.fieldNames(records[i]);
156+
lineCounter += 1;
157+
} else {
158+
this.addRow(records[i]);
159+
lineCounter += 1;
160+
}
161+
}
162+
};
163+
164+
// Create the table from a JSON file
165+
PrettyTable.prototype.json = function(filename) {
166+
var jsondata = JSON.parse(fs.readFileSync(filename, 'utf8'));
167+
for (var i=0; i < jsondata.length; i++) {
168+
rowKeys = Object.keys(jsondata[i]);
169+
rowVals = [];
170+
for (var k=0; k < rowKeys.length; k++) {
171+
rowVals.push(jsondata[i][rowKeys[k]]);
172+
}
173+
if (this.table.columnNames.length === 0) {
174+
this.fieldNames(rowKeys);
175+
}
176+
this.addRow(rowVals);
177+
}
178+
return this.toString();
179+
};
180+
181+
// Sort the table given a column in ascending or descending order
182+
PrettyTable.prototype.sortTable = function(colname, reverse) {
231183
// Find the index of the column given the name
232-
var colindex = table.columnNames.indexOf(colname);
184+
var colindex = this.table.columnNames.indexOf(colname);
233185

234186
// Comparator method which takes the column index and sort direction
235187
function Comparator(a,b){
236-
if (reverse === true) {
188+
if (typeof(reverse) === "boolean" && reverse === true) {
237189
if (a[colindex] < b[colindex]) return 1;
238190
if (a[colindex] > b[colindex]) return -1;
239191
return 0;
@@ -244,7 +196,26 @@ var sortTableByColumn = function(colname, reverse) {
244196
}
245197
}
246198
// Sort array of table rows
247-
table.rows = table.rows.sort(Comparator);
199+
this.table.rows = this.table.rows.sort(Comparator);
248200
};
249201

250-
exports.version = "0.3.0";
202+
// Delete a single row from the table given row number
203+
PrettyTable.prototype.deleteRow = function(rownum) {
204+
if (rownum <= this.table.rows.length && rownum > 0) {
205+
this.table.rows.splice(rownum-1, 1);
206+
}
207+
};
208+
209+
// Clear the contents from the table, but keep columns and structure
210+
PrettyTable.prototype.clearTable = function() {
211+
this.table.rows = [];
212+
};
213+
214+
// Delete the entire table
215+
PrettyTable.prototype.deleteTable = function() {
216+
this.table = {"columnNames": [], "rows": [], "maxWidth": []};
217+
};
218+
219+
PrettyTable.prototype.version = "0.3.0";
220+
221+
module.exports = PrettyTable;

0 commit comments

Comments
 (0)