Permalink
Browse files

Refactor for totals support

  • Loading branch information...
1 parent e808b1f commit d7fd002f67ad59a085204e87fae41a2bcdb51776 @eldargab committed Jul 22, 2012
Showing with 120 additions and 125 deletions.
  1. +71 −0 lib/print.js
  2. +18 −125 lib/table.js
  3. +31 −0 lib/util.js
View
@@ -0,0 +1,71 @@
+var util = require('./util')
+
+module.exports = function print (rows, columns, shift) {
+ 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[col] || columns[col].printer
+ }
+
+ function calcWidths () {
+ for (var key in columns) {
+ setWidth(key, key.length)
+ }
+
+ 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 util.padSpaces(s, width)
+ s = s.slice(0, width)
+ if (width > 3) s = s.slice(0, -3).concat('...')
+ return s
+ }
+
+ var ret = ''
+
+ calcWidths()
+
+ ret += printRow(function (key) {
+ return key
+ })
+
+ ret += printRow(function (key, width) {
+ return util.padWithDashs('', width)
+ })
+
+ rows.forEach(function (row) {
+ ret += printRow(function (key, width) {
+ return cellPrinter(row, key).call(row, row[key], width)
+ })
+ })
+
+ return ret
+}
View
@@ -1,5 +1,18 @@
+var util = require('./util')
+var print = require('./print')
+
module.exports = Table
+Table.string = function (obj) {
+ if (obj === undefined) return ''
+ return String(obj)
+}
+
+Table.padLeft = util.padLeft
+Table.RightPadder = util.RightPadder
+Table.LeftPadder = util.LeftPadder
+
+
function Table () {
this.columns = {}
this.rows = []
@@ -22,145 +35,25 @@ function Row () {
})
}
-Table.prototype.cell = function (col, val, printer, length) {
+Table.prototype.cell = function (col, val, printer, width) {
this.row.__cell(col, val, printer)
var c = this.columns[col] || (this.columns[col] = {
printer: printer || Table.string
})
- c.length = length > c.length || c.length == null ? length : c.length
+ c.width = width > c.width || c.width == null ? width : c.width
return this
}
-Table.prototype.newRow = function () {
+Table.prototype.newRow = Table.prototype.newLine = function () {
this.rows.push(this.row)
this.row = new Row
return this
}
-Table.prototype.newLine = Table.prototype.newRow // backwords compatibility
+Table.prototype.sort = require('./sort')
Table.prototype.shift = ' '
Table.prototype.toString = function () {
- return new Printer(this).print()
-}
-
-Table.prototype.sort = require('./sort')
-
-
-function Printer (table) {
- this.table = table
- this.columns = table.columns
- this.colLengths = {}
- this.shift = table.shift
-}
-
-Printer.prototype.print = function () {
- var padWithDashs = RightPadder('-')
-
- var s = ''
-
- this.calcColumnLengths()
-
- s += this.printRow(function (key) {
- return key
- })
-
- s += this.printRow(function (key, length) {
- return padWithDashs('', length)
- })
-
- this.forEachRow(function (row) {
- s += this.printRow(function (key, length) {
- return this.cellPrinter(row, key).call(row, row[key], length)
- })
- })
-
- return s
-}
-
-Printer.prototype.forEachRow = function (cb) {
- return this.table.rows.forEach(cb, this)
-}
-
-Printer.prototype.cellPrinter = function (row, col) {
- return row.__printers[col] || this.columns[col].printer
-}
-
-
-Printer.prototype.calcColumnLengths = function () {
- var setLength = function (col, length) {
- var isFixed = this.columns[col].length != null
- if (isFixed) {
- this.colLengths[col] = this.columns[col].length
- } else {
- if (this.colLengths[col] > length) return
- this.colLengths[col] = length
- }
- }.bind(this)
-
- for (var key in this.columns) {
- setLength(key, key.length)
- }
-
- this.forEachRow(function (row) {
- for (var key in this.columns) {
- setLength(key, this.cellPrinter(row, key).call(row, row[key]).length)
- }
- })
-}
-
-Printer.prototype.printRow = function (print) {
- var s = ''
- var firstColumn = true
- for (var key in this.columns) {
- if (!firstColumn) s += this.shift
- firstColumn = false
- var l = this.colLengths[key]
- s += printCell(print.call(this, key, l), l)
- }
- s += '\n'
- return s
-}
-
-function printCell (s, length) {
- if (s.length <= length) return padSpaces(s, length)
- s = s.slice(0, length)
- if (length > 3) s = s.slice(0, -3).concat('...')
- return s
-}
-
-var padSpaces = RightPadder()
-
-function RightPadder (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
- }
-}
-
-function LeftPadder (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
- }
-}
-
-
-Table.padLeft = LeftPadder()
-
-Table.string = function (obj) {
- if (obj === undefined) return ''
- return String(obj)
+ return print(this.rows, this.columns, this.shift)
}
View
@@ -0,0 +1,31 @@
+
+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()

0 comments on commit d7fd002

Please sign in to comment.