Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 7d5999af2cad818bb32162a14ba588deccc8621f @eldargab committed Mar 10, 2012
Showing with 131 additions and 0 deletions.
  1. +18 −0 example.js
  2. +96 −0 lib/table.js
  3. +17 −0 package.json
@@ -0,0 +1,18 @@
+var Table = require('./lib/table');
+
+var data = [
+ { id: 123123, desc: 'Something awesome', price: 1000.00 },
+ { id: 245452, desc: 'Very interesting book', price: 11.45},
+ { id: 232323, desc: 'Yet another product', price: 555.55 }
+]
+
+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.newLine();
+});
+
+console.log(t.toString());
@@ -0,0 +1,96 @@
+module.exports = Table;
+
+function Table () {
+ this.shift = ' ';
+ this.columns = {};
+ this.lines = [];
+ 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;
+ if (length != null) {
+ c.length = length;
+ }
+ else {
+ var s = c.printer(obj);
+ c.length = s.length > c.length ? s.length : c.length;
+ }
+}
+
+Table.prototype.newLine = function () {
+ this.lines.push(this.line);
+ this.line = {};
+}
+
+Table.prototype.toString = function () {
+ var self = this;
+ var pad = Table.RightPadder();
+ var padWithDashs = Table.RightPadder('-');
+ var s = '';
+
+ s += this._printLine(function (key, col) {
+ return pad(key, col.length);
+ });
+
+ s += this._printLine(function (key, col) {
+ return padWithDashs('', col.length);
+ });
+
+ this.lines.forEach(function (line) {
+ s += self._printLine(function (key, col) {
+ return pad(col.printer(line[key], col.length), col.length);
+ });
+ });
+
+ return s;
+}
+
+Table.prototype._printLine = function (print) {
+ var s = '';
+ var firstColumn = true;
+ for (var key in this.columns) {
+ if (!firstColumn) s += this.shift;
+ firstColumn = false;
+ s += print(key, this.columns[key]);
+ }
+ 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;
+ }
+}
+
+Table.padLeft = Table.LeftPadder();
+
+Table.RightPadder = function (char) {
+ char = char || ' ';
+ return function (obj, length) {
+ var s = String(obj);
+ var l = s.length;
+ for (var i = 0; i < length - l; i++) {
+ s += char;
+ }
+ return s;
+ }
+}
@@ -0,0 +1,17 @@
+{
+ "author": "Eldar Gabdullin <eldargab@gmail.com>",
+ "name": "asci-table",
+ "description": "Nice asci table for node.js",
+ "version": "0.0.0",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com:eldargab/easy-table.git"
+ },
+ "main": "lib/table.js",
+ "dependencies": {},
+ "devDependencies": {},
+ "optionalDependencies": {},
+ "engines": {
+ "node": "*"
+ }
+}

0 comments on commit 7d5999a

Please sign in to comment.