Permalink
Browse files

Refactoring

  • Loading branch information...
1 parent b1a9729 commit 9020abc647eddfde7aaca54e5ba87c2150ea1be7 @eldargab committed Jul 23, 2012
Showing with 149 additions and 146 deletions.
  1. +2 −6 example.js
  2. +29 −0 lib/aggregations.js
  3. +118 −72 lib/table.js
  4. +0 −68 lib/util.js
View
@@ -11,15 +11,11 @@ var t = new Table
data.forEach(function (product) {
t.cell('Product Id', product.id)
t.cell('Description', product.desc)
- t.cell('Price, USD', product.price, function pricePrinter (price, width) {
- return Table.padLeft(price.toFixed(2), width)
- })
+ t.cell('Price, USD', product.price, Table.Number(2))
t.newRow()
})
t.sort(['Price, USD'])
-t.total('Price, USD', Table.aggr.avg, function (val, width) {
- return Table.padLeft('Avg: ' + val.toFixed(2), width)
-})
+t.total('Price, USD', Table.aggr.avg, Table.aggr.Printer('Avg:', Table.Number(2)))
console.log(t.toString())
View
@@ -0,0 +1,29 @@
+var padLeft = require('./table').padLeft
+
+var Printer = exports.Printer = function (name, format) {
+ return function (val, width) {
+ var s = name + ' ' + format(val)
+ return width == null
+ ? s
+ : padLeft(s, width)
+ }
+}
+
+
+exports.sum = function (sum, val) {
+ sum = sum || 0
+ return sum += val
+}
+
+exports.sum.printer = Printer('\u2211', String)
+
+
+exports.avg = function (sum, val, index, length) {
+ sum = sum || 0
+ sum += val
+ return index + 1 == length
+ ? sum / length
+ : sum
+}
+
+exports.avg.printer = Printer('Avg:', String)
View
@@ -1,21 +1,48 @@
-var util = require('./util')
-
module.exports = Table
-Table.string = util.string
-Table.number = util.number
-Table.padLeft = util.padLeft
-Table.RightPadder = util.RightPadder
-Table.LeftPadder = util.LeftPadder
-Table.aggr = util.aggr
+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)
+ }
+}
-function Table () {
- this.columns = {}
- this.rows = []
- this.row = new Row
+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.Row = Row
function Row () {
Object.defineProperties(this, {
__printers: {
@@ -32,66 +59,10 @@ function 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.prototype.total = function (col, fn, printer) {
- fn = fn || util.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.toString = function () {
- var delimeter = this._row(function () {
- return ['', util.padWithDashs]
- })
- var head = this._row(function (key) {
- return [key, Table.string]
- })
- 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._row = 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
-}
+Table.print = print
function print (rows, columns, shift) {
+ var padSpaces = Table.RightPadder()
var widths = {}
function setWidth (col, width) {
@@ -105,7 +76,7 @@ function print (rows, columns, shift) {
}
function cellPrinter (row, col) {
- return row.__printers[col] || util.string
+ return (row.__printers && row.__printers[col]) || Table.string
}
function calcWidths () {
@@ -134,7 +105,7 @@ function print (rows, columns, shift) {
}
function printCell (s, width) {
- if (s.length <= width) return util.padSpaces(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
@@ -147,4 +118,79 @@ function print (rows, columns, shift) {
return cellPrinter(row, key).call(row, row[key], width)
})
}).join('')
+
+}
+
+
+function Table () {
+ this.columns = {}
+ this.rows = []
+ 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.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.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
}
View
@@ -1,68 +0,0 @@
-exports.RightPadder = function (char) {
- char = char || ' '
- return function (obj, length) {
- var s = String(obj)
- var l = s.length
- for (var i = 0; i < length - l; i++) {
- s += char
- }
- return s
- }
-}
-
-exports.LeftPadder = function (char) {
- char = char || ' '
- return function (obj, length) {
- var ret = ''
- var s = String(obj)
- for (var i = 0; i < length - s.length; i++) {
- ret += char
- }
- ret += s
- return ret
- }
-}
-
-exports.padLeft = exports.LeftPadder()
-
-exports.padWithDashs = exports.RightPadder('-')
-
-exports.padSpaces = exports.RightPadder()
-
-exports.string = function (val) {
- if (val === undefined) return ''
- return String(val)
-}
-
-exports.number = function (val, width) {
- return exports.padLeft(exports.string(val), width)
-}
-
-
-var aggr = exports.aggr = {}
-
-aggr.Printer = function (name, format) {
- return function (val, width) {
- var s = name + ' ' + format(val)
- return width == null
- ? s
- : exports.padLeft(s, width)
- }
-}
-
-aggr.sum = function (sum, val) {
- sum = sum || 0
- return sum += val
-}
-
-aggr.sum.printer = aggr.Printer('\u2211', String)
-
-aggr.avg = function (sum, val, index, length) {
- sum = sum || 0
- sum += val
- return index + 1 == length
- ? sum / length
- : sum
-}
-
-aggr.avg.printer = aggr.Printer('Avg:', String)

0 comments on commit 9020abc

Please sign in to comment.