Skip to content
Browse files

add support for printing transposed version (Closes #2)

  • Loading branch information...
1 parent 36e9616 commit 3d404bc37b98c8727e227c775dc40f52bf8fa60a @eldargab committed Jul 25, 2012
Showing with 32 additions and 0 deletions.
  1. +2 −0 example.js
  2. +21 −0 lib/table.js
  3. +9 −0 test/table.js
View
2 example.js
@@ -19,3 +19,5 @@ t.sort(['Price, USD'])
t.total('Price, USD', Table.aggr.avg, Table.aggr.Printer('Avg:', Table.Number(2)))
console.log(t.toString())
+console.log(t.print())
+console.log(t.printTransposed(' : '))
View
21 lib/table.js
@@ -173,6 +173,27 @@ 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 () {
View
9 test/table.js
@@ -30,6 +30,15 @@ describe('Easy table', function () {
)
})
+ it('Printing transposed version', function () {
+ t.cell('c1', 11).cell('c2', 12).newRow()
+ t.cell('c1', 21).cell('c2', 22).newRow()
+ t.printTransposed(':').should.equal(
+ 'c1:11:21\n' +
+ 'c2:12:22\n'
+ )
+ })
+
it('Should adjust column width to fit all contents', function () {
t.cell('col', '').newRow()
expectLine(1).be.equal('col')

0 comments on commit 3d404bc

Please sign in to comment.
Something went wrong with that request. Please try again.