-
Notifications
You must be signed in to change notification settings - Fork 4
/
columnizer.js
75 lines (64 loc) · 2.09 KB
/
columnizer.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
var _ = require('underscore'),
colors = require('colors');
// remove terminal escape characters before returning length
function len(string) {
return string.replace(/\033\[[0-9;]*m/g, '').length;
}
// pad
function pad(string, target) {
return string + (new Array(target - len(string)).join(' '));
}
var Columnizer = function Columnizer(table) {
var keys,
self = this;
if (table && table[0] instanceof Array) {
this.table = table;
} else if (table && typeof table[0] === 'object') {
keys = _.keys(table[0]);
this.table = [keys];
_.each(table, function (object) {
var row = [],
i = 0,
l = keys.length;
for (; i < l ; i++) {
row.push(object[keys[i]]);
}
self.table.push(row);
});
} else {
this.table = [];
}
};
Columnizer.prototype.row = function (args) {
this.table.push(Array.prototype.slice.call(arguments, 0));
};
Columnizer.prototype.print = function (columnPadding, headers) {
console.log(this._toString(columnPadding, headers));
};
Columnizer.prototype.length = function () {
return this.table.length;
};
Columnizer.prototype._toString = function (columnPadding, headers) {
var padding = columnPadding || 5,
totalColumns = _.max(this.table, function (item) { return item.length; }).length,
colWidths = [],
result = [];
// first we figure out what the max length is for each column
_.each(this.table, function (row, index) {
_.each(row, function (item, i) {
var str = item.toString();
colWidths[i] = ((colWidths[i] || 0) < len(str)) ? len(str) : colWidths[i];
});
});
// now we can build our table
_.each(this.table, function (row, index) {
_.each(row, function (item, i) {
row[i] = pad(item.toString(), colWidths[i] + padding);
if (index === 0 && headers) row[i] = row[i].bold;
});
result.push(row.join(''));
});
return result.join('\n');
};
// our export
module.exports = Columnizer;