Skip to content

Commit

Permalink
Release 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
eldargab committed Jul 23, 2012
1 parent 9020abc commit 36e9616
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .npmignore
@@ -1,2 +1,3 @@
test/
example.js
example.js
HISTORY.md
14 changes: 14 additions & 0 deletions HISTORY.md
@@ -0,0 +1,14 @@
# 0.1.0

* Added: `.total()` function
* Change: printers are stored on per cell basis
* Change: `.newLine()` renamed to `.newRow()`
* Mark `.rows` and `.columns` as a public api

# 0.0.2

* Added: `.sort()` function

# 0.0.1

* Initial release
75 changes: 74 additions & 1 deletion README.md
Expand Up @@ -18,7 +18,7 @@ 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.toFixed(2), Table.padLeft);
t.cell('Price, USD', product.price, Table.Number(2));
t.newRow();
});

Expand Down Expand Up @@ -53,6 +53,24 @@ function is called to get minimal width required to fit cell correctly, at the
second phase `printer` function is called to get actual string to render with
additional `width` parameter supplied.

Example:

``` javascript
// Coloring too big numbers in red
require('colors')

function markTooBigs (val, width) {
if (width == null) return Table.string(val)
return val > 100
? val.toString().red
: Table.string(val)
}
...
t.cell('foo', val, markTooBigs)
```

### Sorting

You can sort a table by calling `.sort()`, and optionally passing in a list of
column names to sort on (by default uses all columns), or a custom comparator
function. It is also possible to specify the sort order. For example:
Expand All @@ -63,6 +81,57 @@ t.sort(['Price, USD|asc']) // will sort in ascending order
t.sort(['Price, USD']) // sorts in ascending order by default
```

### Totaling

Easy table can help you to calculate and render totals:

``` javascript
t.total('Price, USD', function accumulator (sum, val, index, length) {
sum = sum || 0
sum += val
return index + 1 == length
? sum / length
: sum
}, function print (val, width) {
var s = 'Avg: ' + Table.Number(2)
return width == null
? s
: Table.padLeft(s, width)
})
```

```
Product Id Description Price, USD
---------- --------------------- -----------
245452 Very interesting book 11.45
232323 Yet another product 555.55
123123 Something awesome 1000.00
---------- --------------------- -----------
Avg: 522.33
```

`total()` function also accepts printer via `printer` property of
accumulator, so it is possible to create reusable aggregations like this:

``` javascript
var priceAvg = function (sum, val, index, length) {
sum = sum || 0
sum += val
return index + 1 == length
? sum / length
: sum
}

priceAvg.printer = function print (val, width) {
var s = 'Avg: ' + Table.Number(2)
return width == null
? s
: Table.padLeft(s, width)
}

t.total('Price', priceAvg)
```

## Installation

Just install from the npm repository with:
Expand All @@ -71,6 +140,10 @@ Just install from the npm repository with:
$ npm install easy-table
```

## TODO

Full api docs

## License

(The MIT License)
Expand Down
6 changes: 4 additions & 2 deletions lib/table.js
Expand Up @@ -123,8 +123,8 @@ function print (rows, columns, shift) {


function Table () {
this.columns = {}
this.rows = []
this.columns = {} /* @api: public */
this.rows = [] /* @api: public */
this._row = new Row
}

Expand All @@ -146,6 +146,8 @@ 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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -3,7 +3,7 @@
"name": "easy-table",
"description": "Nice text table for the CLI",
"keywords": ["table", "text", "cli"],
"version": "0.0.3",
"version": "0.1.0",
"repository": {
"type": "git",
"url": "https://eldargab@github.com/eldargab/easy-table.git"
Expand Down

0 comments on commit 36e9616

Please sign in to comment.