Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add support for printing transposed version (Closes #2)
  • Loading branch information
eldargab committed Jul 25, 2012
1 parent 36e9616 commit 3d404bc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions example.js
Expand Up @@ -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(' : '))
21 changes: 21 additions & 0 deletions lib/table.js
Expand Up @@ -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 () {
Expand Down
9 changes: 9 additions & 0 deletions test/table.js
Expand Up @@ -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')
Expand Down

0 comments on commit 3d404bc

Please sign in to comment.