Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added: .sort() function

  • Loading branch information...
commit dbde494756cdb12744ada0c84d405b08814e4b55 1 parent b87b4d4
@eldargab authored
Showing with 71 additions and 1 deletion.
  1. +10 −0 README.md
  2. +5 −1 example.js
  3. +40 −0 lib/table.js
  4. +16 −0 test/table.js
View
10 README.md
@@ -53,6 +53,16 @@ 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.
+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:
+
+``` javascript
+t.sort(['Price, USD|des']) // will sort in descending order
+t.sort(['Price, USD|asc']) // will sort in ascending order
+t.sort(['Price, USD']) // sorts in ascending order by default
+```
+
## Installation
Just install from the npm repository with:
View
6 example.js
@@ -11,8 +11,12 @@ 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, function pricePrinter (price, width) {
+ return Table.padLeft(price.toFixed(2));
+ });
t.newLine();
});
+t.sort(['Price, USD']);
+
console.log(t.toString());
View
40 lib/table.js
@@ -104,3 +104,43 @@ Table.RightPadder = function (char) {
return s;
}
}
+
+Table.prototype.sort = function (comparator) {
+ if (typeof comparator != 'function') {
+ var sortKeys = Array.isArray(comparator)
+ ? comparator
+ : Object.keys(this.columns);
+ comparator = KeysComparator(sortKeys);
+ }
+ this.lines.sort(comparator);
+ return this;
+}
+
+function KeysComparator (keys) {
+ var comparators = keys.map(function (key) {
+ var sortFn = 'asc';
+
+ var m = /(.*)\|\s*(asc|des)\s*$/.exec(key);
+ if (m) {
+ key = m[1];
+ sortFn = m[2];
+ }
+
+ return function compare (a, b) {
+ var va = a[key];
+ var vb = b[key];
+ var ret = 0;
+ if (va > vb) ret = 1;
+ if (va < vb) ret = -1;
+ return sortFn == 'asc' ? ret : -1 * ret;
+ }
+ });
+
+ return function compare (a, b) {
+ for (var i = 0; i < comparators.length; i++) {
+ var res = comparators[i](a, b)
+ if (res != 0) return res;
+ }
+ return 0;
+ }
+}
View
16 test/table.js
@@ -80,5 +80,21 @@ describe('Easy table', function () {
it('test Table.padLeft()', function () {
Table.padLeft('a', 2).should.equal(' a');
});
+
+ it('test .sort() function', function () {
+ t.cell('a', 1).newLine();
+ t.cell('a', 2).newLine();
+ t.sort(['a|des']);
+ expectLine(3).be.equal('2');
+ expectLine(4).be.equal('1');
+
+ t.sort(['a']);
+ expectLine(3).be.equal('1');
+ expectLine(4).be.equal('2');
+
+ t.sort(['a|des']).sort(['a|asc']);
+ expectLine(3).be.equal('1');
+ expectLine(4).be.equal('2');
+ });
});
Please sign in to comment.
Something went wrong with that request. Please try again.