Permalink
Browse files

Refactoring

  • Loading branch information...
1 parent dbde494 commit f199b9be4bd5968fedde7c542c73c8d3e61d6d6a @eldargab committed Mar 20, 2012
Showing with 74 additions and 71 deletions.
  1. +41 −0 lib/sort.js
  2. +33 −71 lib/table.js
View
@@ -0,0 +1,41 @@
+module.exports = sort;
+
+function sort (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
@@ -7,14 +7,11 @@ function Table () {
this.line = {};
}
-Table.prototype.column = function (key) {
- return this.columns[key] || (this.columns[key] = {length: key.length})
-}
-
Table.prototype.cell = function (col, obj, printer, length) {
- var c = this.column(col);
- c.printer = printer || Table.string;
this.line[col] = obj;
+
+ var c = this.columns[col] || (this.columns[col] = {length: col.length});
+ c.printer = printer || Table.string;
if (length != null) {
c.length = length;
}
@@ -31,22 +28,17 @@ Table.prototype.newLine = function () {
return this;
}
+Table.prototype.sort = require('./sort');
+
+
Table.prototype.toString = function () {
var self = this;
- var pad = Table.RightPadder();
- var padWithDashs = Table.RightPadder('-');
-
- function printCell (s, length) {
- if (s.length <= length) return pad(s, length);
- s = s.slice(0, length);
- if (length > 3) s = s.slice(0, -3).concat('...');
- return s;
- }
+ var padWithDashs = RightPadder('-');
var s = '';
- s += this._printLine(function (key, col) {
- return printCell(key, col.length);
+ s += this._printLine(function (key) {
+ return key;
});
s += this._printLine(function (key, col) {
@@ -55,7 +47,7 @@ Table.prototype.toString = function () {
this.lines.forEach(function (line) {
s += self._printLine(function (key, col) {
- return printCell(col.printer(line[key], col.length), col.length);
+ return col.printer(line[key], col.length);
});
});
@@ -68,32 +60,23 @@ Table.prototype._printLine = function (print) {
for (var key in this.columns) {
if (!firstColumn) s += this.shift;
firstColumn = false;
- s += print(key, this.columns[key]);
+ var col = this.columns[key];
+ s += printCell(print(key, col), col.length);
}
s += '\n';
return s;
}
-Table.string = function (obj) {
- return String(obj);
-}
-
-Table.LeftPadder = function (char) {
- char = char || ' ';
- return function (obj, length) {
- var ret = '';
- var s = String(obj);
- for (var i = 0; i < length - s.length; i++) {
- ret += char;
- }
- ret += s;
- return ret;
- }
+function printCell (s, length) {
+ if (s.length <= length) return padSpaces(s, length);
+ s = s.slice(0, length);
+ if (length > 3) s = s.slice(0, -3).concat('...');
+ return s;
}
-Table.padLeft = Table.LeftPadder();
+var padSpaces = RightPadder();
-Table.RightPadder = function (char) {
+function RightPadder (char) {
char = char || ' ';
return function (obj, length) {
var s = String(obj);
@@ -105,42 +88,21 @@ Table.RightPadder = function (char) {
}
}
-Table.prototype.sort = function (comparator) {
- if (typeof comparator != 'function') {
- var sortKeys = Array.isArray(comparator)
- ? comparator
- : Object.keys(this.columns);
- comparator = KeysComparator(sortKeys);
+function LeftPadder (char) {
+ char = char || ' ';
+ return function (obj, length) {
+ var ret = '';
+ var s = String(obj);
+ for (var i = 0; i < length - s.length; i++) {
+ ret += char;
+ }
+ ret += s;
+ return ret;
}
- 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;
- }
- });
+Table.padLeft = LeftPadder();
- 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;
- }
-}
+Table.string = function (obj) {
+ return String(obj);
+}

0 comments on commit f199b9b

Please sign in to comment.