Comparing version 0.0.1 to 0.0.2
43
index.js
@@ -22,6 +22,45 @@ 'use strict'; | ||
Tabular.prototype.push = function (row) { | ||
this._rows.push(row); | ||
Tabular.prototype.push = function () { | ||
Array.prototype.push.apply(this._rows, arguments); | ||
}; | ||
var sortTypes = { | ||
first: function (a, b) { | ||
if (a[0] < b[0]) { | ||
return -1; | ||
} | ||
if (a[0] > b[0]) { | ||
return 1; | ||
} | ||
return 0; | ||
}, | ||
second: function (a, b) { | ||
if (a[1] < b[1]) { | ||
return -1; | ||
} | ||
if (a[1] > b[1]) { | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
}; | ||
Tabular.prototype.sort = function (type) { | ||
if (!type) { | ||
type = 'first'; | ||
} | ||
if (!sortTypes[type]) { | ||
throw new Error('Invalid sort type'); | ||
} | ||
this._rows = this._rows.sort(sortTypes[type]); | ||
return this; | ||
}; | ||
Tabular.prototype.get = function () { | ||
@@ -28,0 +67,0 @@ if (this._rows.length === 0) { |
{ | ||
"name": "tabular", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Create fluid lists in which the first column adapts to the content", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -25,2 +25,15 @@ node-tabular | ||
console.log(tab.get()); | ||
``` | ||
``` | ||
### Sorting | ||
You can also sort the list by the `first` or `second` column like so: | ||
```js | ||
// ... | ||
console.log(tab.sort('second').get()); | ||
``` | ||
Note that once you sort the list, the original order is lost. |
4864
71
38