Skip to content

Commit 2743625

Browse files
committed
fixed all lint issues in prettytable.js
1 parent e828128 commit 2743625

1 file changed

Lines changed: 95 additions & 74 deletions

File tree

prettytable.js

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

4-
var PrettyTable = function() {
4+
var PrettyTable = function () {
55
// Skeleton structure of table with list of column names, row and max width of each column element
66
this.table = {
7-
"columnNames": [],
8-
"rows": [],
9-
"maxWidth": []
7+
'columnNames': [],
8+
'rows': [],
9+
'maxWidth': []
1010
};
11+
this.version = '0.3.0';
1112
};
1213

1314
// Define list of columns for the table
14-
PrettyTable.prototype.fieldNames = function(names) {
15+
PrettyTable.prototype.fieldNames = function (names) {
1516
this.table.columnNames = names;
16-
for (var i=0; i < names.length; i++) {
17+
for (var i = 0; i < names.length; i++) {
1718
this.table.maxWidth.push(names[i].length);
1819
}
1920
};
2021

2122
// Add a single row to the table
22-
PrettyTable.prototype.addRow = function(row) {
23+
PrettyTable.prototype.addRow = function (row) {
2324
this.table.rows.push(row);
24-
for (var i=0; i < row.length; i++) {
25+
for (var i = 0; i < row.length; i++) {
2526
if (row[i].toString().length > this.table.maxWidth[i]) {
2627
this.table.maxWidth[i] = row[i].toString().length;
2728
}
2829
}
2930
};
3031

3132
// Single function to create table when headers and array of rows passed
32-
PrettyTable.prototype.create = function(headers, rows) {
33+
PrettyTable.prototype.create = function (headers, rows) {
3334
// Add table headers
3435
this.fieldNames(headers);
3536

3637
// Add rows one by one
37-
for (var i=0; i < rows.length; i++) {
38+
for (var i = 0; i < rows.length; i++) {
3839
this.addRow(rows[i]);
3940
}
4041
};
4142

4243
// Convert the table to string
43-
PrettyTable.prototype.toString = function() {
44+
PrettyTable.prototype.toString = function () {
45+
var finalTable = '';
46+
var columnString = '| ';
47+
var rowString = '';
48+
var lengthDifference = '';
49+
4450
// 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++) {
51+
var drawLine = function (table) {
52+
var arrayLength = 0;
53+
for (var i = 0; i < table.maxWidth.length; i++) {
4854
arrayLength += table.maxWidth[i];
4955
}
5056
return '+' + Array(arrayLength + table.maxWidth.length * 3).join('-') + '+';
5157
};
5258

53-
finalTable = "";
54-
5559
// If no columns present, return empty string
5660
if (this.table.columnNames.length === 0) {
5761
return finalTable;
5862
}
5963

6064
// Create the table header from column list
61-
columnString = "| ";
62-
rowString = "";
63-
for (var i=0; i < this.table.columnNames.length; i++) {
65+
for (var i = 0; i < this.table.columnNames.length; i++) {
6466
columnString += this.table.columnNames[i];
6567
// Adjust for max width of the column and pad spaces
6668
if (this.table.columnNames[i].length < this.table.maxWidth[i]) {
6769
lengthDifference = this.table.maxWidth[i] - this.table.columnNames[i].length;
6870
columnString += Array(lengthDifference + 1).join(' ');
6971
}
70-
columnString += " | ";
72+
columnString += ' | ';
7173
}
72-
finalTable += drawLine(this.table) + "\n";
73-
finalTable += columnString + "\n";
74-
finalTable += drawLine(this.table) + "\n";
74+
finalTable += drawLine(this.table) + '\n';
75+
finalTable += columnString + '\n';
76+
finalTable += drawLine(this.table) + '\n';
7577

7678
// Construct the table body
77-
for (i=0; i < this.table.rows.length; i++) {
78-
var tempRowString = "| ";
79-
for (var k=0; k < this.table.rows[i].length; k++) {
79+
for (i = 0; i < this.table.rows.length; i++) {
80+
var tempRowString = '| ';
81+
for (var k = 0; k < this.table.rows[i].length; k++) {
8082
tempRowString += this.table.rows[i][k];
8183
// Adjust max width of each cell and pad spaces as necessary
8284
if (this.table.rows[i][k].toString().length < this.table.maxWidth[k]) {
8385
lengthDifference = this.table.maxWidth[k] - this.table.rows[i][k].toString().length;
8486
tempRowString += Array(lengthDifference + 1).join(' ');
8587
}
86-
tempRowString += " | ";
88+
tempRowString += ' | ';
8789
}
88-
rowString += tempRowString + "\n";
90+
rowString += tempRowString + '\n';
8991
}
9092
// Remove newline from the end of the table string
9193
rowString = rowString.slice(0, -1);
9294
// Append to the final table string
93-
finalTable += rowString + "\n";
95+
finalTable += rowString + '\n';
9496
// Draw last line and return
95-
finalTable += drawLine(this.table) + "\n";
97+
finalTable += drawLine(this.table) + '\n';
9698
return finalTable;
9799
};
98100

99101
// Write the table string to the console
100-
PrettyTable.prototype.print = function() {
102+
PrettyTable.prototype.print = function () {
101103
console.log(this.toString());
102104
};
103105

104106
// Write the table string to the console as HTML table formats
105-
PrettyTable.prototype.html = function(attributes) {
107+
PrettyTable.prototype.html = function (attributes) {
106108
// If attributes provided, add them as inline properties, else create default table tag
107-
var htmlTable = "";
108-
if (typeof attributes == "undefined") {
109-
htmlTable = "<table>";
110-
} else {
109+
var htmlTable = '';
110+
if (typeof attributes == 'undefined') {
111+
htmlTable = '<table>';
112+
}
113+
else {
111114
var attributeList = [];
112115
for (var key in attributes) {
113-
attributeList.push(key + '="' + attributes[key] + '"');
116+
attributeList.push(key + '=\'' + attributes[key] + '\'');
114117
}
115-
var attributeString = attributeList.join(" ");
116-
htmlTable = "<table " + attributeString + ">";
118+
var attributeString = attributeList.join(' ');
119+
htmlTable = '<table ' + attributeString + '>';
117120
}
118121

119122
// Define the table headers in <thead> from table column list
120-
var tableHead = "<thead><tr>";
121-
for (var i=0; i < this.table.columnNames.length; i++) {
122-
var headerString = "<th>" + this.table.columnNames[i] + "</th>";
123+
var tableHead = '<thead><tr>';
124+
for (var i = 0; i < this.table.columnNames.length; i++) {
125+
var headerString = '<th>' + this.table.columnNames[i] + '</th>';
123126
tableHead += headerString;
124127
}
125-
tableHead += "</tr></thead>";
128+
tableHead += '</tr></thead>';
126129
htmlTable += tableHead;
127130

128131
// Construct the table body from the array of rows
129-
var tableBody = "<tbody>";
130-
for (i=0; i < this.table.rows.length; i++) {
131-
var rowData = "<tr>";
132-
for (var k=0; k < this.table.rows[i].length; k++) {
133-
var cellData = "<td>" + this.table.rows[i][k] + "</td>";
132+
var tableBody = '<tbody>';
133+
for (i = 0; i < this.table.rows.length; i++) {
134+
var rowData = '<tr>';
135+
for (var k = 0; k < this.table.rows[i].length; k++) {
136+
var cellData = '<td>' + this.table.rows[i][k] + '</td>';
134137
rowData += cellData;
135138
}
136-
rowData += "</tr>";
139+
rowData += '</tr>';
137140
tableBody += rowData;
138141
}
139142
// Close all tags and return
140-
tableBody += "</tbody>";
143+
tableBody += '</tbody>';
141144
htmlTable += tableBody;
142-
htmlTable += "</table>";
145+
htmlTable += '</table>';
143146

144147
return htmlTable;
145148
};
146149

147150
// Create the table from a CSV file
148-
PrettyTable.prototype.csv = function(filename) {
151+
PrettyTable.prototype.csv = function (filename) {
149152
var csvdata = fs.readFileSync(filename, 'utf8');
150153
var records = parse(csvdata);
151154

152155
var lineCounter = 0;
153-
for (var i=0; i < records.length; i++) {
156+
for (var i = 0; i < records.length; i++) {
154157
if (lineCounter === 0) {
155158
this.fieldNames(records[i]);
156159
lineCounter += 1;
157-
} else {
160+
}
161+
else {
158162
this.addRow(records[i]);
159163
lineCounter += 1;
160164
}
161165
}
162166
};
163167

164168
// Create the table from a JSON file
165-
PrettyTable.prototype.json = function(filename) {
169+
PrettyTable.prototype.json = function (filename) {
170+
var rowKeys = '';
171+
var rowVals = '';
166172
var jsondata = JSON.parse(fs.readFileSync(filename, 'utf8'));
167-
for (var i=0; i < jsondata.length; i++) {
173+
for (var i = 0; i < jsondata.length; i++) {
168174
rowKeys = Object.keys(jsondata[i]);
169175
rowVals = [];
170-
for (var k=0; k < rowKeys.length; k++) {
176+
for (var k = 0; k < rowKeys.length; k++) {
171177
rowVals.push(jsondata[i][rowKeys[k]]);
172178
}
173179
if (this.table.columnNames.length === 0) {
@@ -178,43 +184,58 @@ PrettyTable.prototype.json = function(filename) {
178184
};
179185

180186
// Sort the table given a column in ascending or descending order
181-
PrettyTable.prototype.sortTable = function(colname, reverse) {
187+
PrettyTable.prototype.sortTable = function (colname, reverse) {
182188
// Find the index of the column given the name
183189
var colindex = this.table.columnNames.indexOf(colname);
184190

185191
// Comparator method which takes the column index and sort direction
186-
function Comparator(a,b){
187-
if (typeof(reverse) === "boolean" && reverse === true) {
188-
if (a[colindex] < b[colindex]) return 1;
189-
if (a[colindex] > b[colindex]) return -1;
190-
return 0;
191-
} else {
192-
if (a[colindex] < b[colindex]) return -1;
193-
if (a[colindex] > b[colindex]) return 1;
194-
return 0;
192+
var Comparator = function ( a, b) {
193+
if (typeof reverse === 'boolean' && reverse === true) {
194+
if (a[colindex] < b[colindex]) {
195+
return 1;
196+
}
197+
else if (a[colindex] > b[colindex]) {
198+
return -1;
199+
}
200+
else {
201+
return 0;
202+
}
203+
}
204+
else {
205+
if (a[colindex] < b[colindex]) {
206+
return -1;
207+
}
208+
else if (a[colindex] > b[colindex]) {
209+
return 1;
210+
}
211+
else {
212+
return 0;
213+
}
195214
}
196215
}
197216
// Sort array of table rows
198217
this.table.rows = this.table.rows.sort(Comparator);
199218
};
200219

201220
// Delete a single row from the table given row number
202-
PrettyTable.prototype.deleteRow = function(rownum) {
221+
PrettyTable.prototype.deleteRow = function (rownum) {
203222
if (rownum <= this.table.rows.length && rownum > 0) {
204-
this.table.rows.splice(rownum-1, 1);
223+
this.table.rows.splice(rownum - 1, 1);
205224
}
206225
};
207226

208227
// Clear the contents from the table, but keep columns and structure
209-
PrettyTable.prototype.clearTable = function() {
228+
PrettyTable.prototype.clearTable = function () {
210229
this.table.rows = [];
211230
};
212231

213232
// Delete the entire table
214-
PrettyTable.prototype.deleteTable = function() {
215-
this.table = {"columnNames": [], "rows": [], "maxWidth": []};
233+
PrettyTable.prototype.deleteTable = function () {
234+
this.table = {
235+
'columnNames': [],
236+
'rows': [],
237+
'maxWidth': []
238+
};
216239
};
217240

218-
PrettyTable.prototype.version = "0.3.0";
219-
220241
module.exports = PrettyTable;

0 commit comments

Comments
 (0)