/
table.js
260 lines (218 loc) · 6.08 KB
/
table.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
module.exports = Table
Table.string = function (val) {
if (val === undefined) return ''
return String(val)
}
Table.Number = function (digits) {
return function (val, width) {
if (val === undefined) return ''
if (typeof val != 'number')
throw new Error(String(val) + ' is not a number')
var s = digits == null ? String(val) : val.toFixed(digits).toString()
return Table.padLeft(s, width)
}
}
Table.RightPadder = function (char) {
char = char || ' '
return function (val, length) {
var s = String(val)
var l = s.length
for (var i = 0; i < length - l; i++) {
s += char
}
return s
}
}
Table.LeftPadder = function (char) {
char = char || ' '
return function (val, length) {
var ret = ''
var s = String(val)
for (var i = 0; i < length - s.length; i++) {
ret += char
}
ret += s
return ret
}
}
Table.padLeft = Table.LeftPadder()
Table.printArray = function (arr, format, cb) {
format = typeof format == 'function' ? format : Formatter(format)
cb = cb || function (t) {
return t.toString()
}
var t = new Table
var cell = t.cell.bind(t)
arr.forEach(function (obj) {
format(obj, cell)
t.newRow()
})
return cb(t)
}
Table.printObj = function (obj, format, cb) {
format = typeof format == 'function' ? format : Formatter(format)
cb = cb || function (t) {
return t.printTransposed(' : ')
}
var t = new Table
format(obj, t.cell.bind(t))
t.newRow()
return cb(t)
}
function Formatter (opts) {
opts = opts || {}
return function (obj, cell) {
for (var key in obj) {
if (!obj.hasOwnProperty(key)) continue
var o = opts[key]
cell(
(o && o.name) || key,
obj[key],
o && o.printer,
o && o.width
)
}
}
}
Table.Row = Row
function Row () {
Object.defineProperties(this, {
__printers: {
value: {},
enumerable: false
},
__cell: {
value: function (col, val, printer) {
this[col] = val
this.__printers[col] = printer
},
enumerable: false
}
})
}
Table.print = print
function print (rows, columns, shift) {
var padSpaces = Table.RightPadder()
var widths = {}
function setWidth (col, width) {
var isFixed = columns[col].width != null
if (isFixed) {
widths[col] = columns[col].width
} else {
if (widths[col] > width) return
widths[col] = width
}
}
function cellPrinter (row, col) {
return (row.__printers && row.__printers[col]) || Table.string
}
function calcWidths () {
rows.forEach(function (row) {
for (var key in columns) {
setWidth(key, cellPrinter(row, key).call(row, row[key]).length)
}
})
}
function printRow (cb) {
var s = ''
var firstColumn = true
for (var key in columns) {
if (!firstColumn) s += shift
firstColumn = false
var width = widths[key]
s += printCell(cb(key, width), width)
}
s += '\n'
return s
}
function printCell (s, width) {
if (s.length <= width) return padSpaces(s, width)
s = s.slice(0, width)
if (width > 3) s = s.slice(0, -3).concat('...')
return s
}
calcWidths()
return rows.map(function (row) {
return printRow(function (key, width) {
return cellPrinter(row, key).call(row, row[key], width)
})
}).join('')
}
function Table () {
this.columns = {} /* @api: public */
this.rows = [] /* @api: public */
this._row = new Row
}
Table.prototype.cell = function (col, val, printer, width) {
this._row.__cell(col, val, printer)
var c = this.columns[col] || (this.columns[col] = {})
if (width != null) c.width = width
return this
}
Table.prototype.newRow = Table.prototype.newLine = function () {
this.rows.push(this._row)
this._row = new Row
return this
}
Table.prototype.sort = require('./sort')
Table.aggr = require('./aggregations')
Table.prototype.totals = null /* @api: public */
Table.prototype.total = function (col, fn, printer) {
fn = fn || Table.aggr.sum
printer = printer || fn.printer
this.totals = this.totals || new Row
var val
var rows = this.rows
this.totals.__cell(col, null, function (_, width) {
if (width != null) return printer(val, width)
val = rows.reduce(function (val, row, index) {
return fn(val, row[col], index, rows.length)
}, null)
return printer(val)
})
return this
}
Table.prototype.shift = ' '
Table.prototype.print = function () {
return print(this.rows, this.columns, this.shift)
}
Table.prototype.printTransposed = function (delimeter) {
var t = new Table
if (delimeter) t.shift = delimeter
function Printer (row, key) {
var p = row.__printers && row.__printers[key]
if (p) return function (val) {
return p(val)
}
}
for (var key in this.columns) {
t.cell('h', key)
this.rows.forEach(function (row, index) {
t.cell('f' + index, row[key], Printer(row, key))
})
t.newRow()
}
return t.print()
}
Table.prototype.toString = function () {
var padWithDashs = Table.RightPadder('-')
var delimeter = this.createRow(function () {
return ['', padWithDashs]
})
var head = this.createRow(function (key) {
return [key]
})
var rows = [head, delimeter].concat(this.rows)
if (this.totals) {
rows = rows.concat([delimeter, this.totals])
}
return print(rows, this.columns, this.shift)
}
Table.prototype.createRow = function (cb) {
var row = new Row
for (var key in this.columns) {
var args = cb(key)
row.__cell(key, args[0], args[1])
}
return row
}