Skip to content

Commit

Permalink
Merge pull request #8 from takamin/issue4-autoAlign
Browse files Browse the repository at this point in the history
issue4: The autoAlign mode is available
  • Loading branch information
takamin committed Apr 24, 2016
2 parents 7f920f9 + eae9411 commit 49ac20d
Show file tree
Hide file tree
Showing 5 changed files with 295 additions and 41 deletions.
115 changes: 83 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,7 @@ Each columns in all rows are aligned in vertical.

You can put it to the console or a preformated text-file.

METHODS
-------

### buffer()

Creates a `ListItBuffer` instance and returns it.

The instance has current row that is a position for the columns to be added.

You cannot edit the columns and rows that were already added.

See the examples below.

### ListItBuffer.d(string)

Adds a new column to the current row.
The `d` is representing 'data'.

Returns `this` object. So you can chain a method call.

### ListItBuffer.nl()

Ends up a process for the current row.

Returns `this` object.

### ListItBuffer.toString()

Returns preformatted text table.

When a autoAlign option is set, the numbers are aligned by its fraction point.

SAMPLE
------
Expand Down Expand Up @@ -80,8 +51,88 @@ $ node sample/japanese-food.js
5 Sashimi Very fresh sliced fish Try it now, It's good
```

METHOD
------
### autoAlign

__planets.js__

```
var listit = require("list-it");
var buf = listit.buffer({ "autoAlign" : true });
console.log(
buf
.d("NAME").d("Mass(10^24kg)").d("Dia(km)")
.d("Dens(kg/m3)").d("Grav(m/s2)")
.d("EscV(km/s)").d("Rot(hours)").nl()
.d("MERCURY").d(0.33).d(4879).d(5427).d(3.7).d(4.3).d(1407.6).nl()
.d("VENUS").d(4.87).d(12104).d(5243).d(8.9).d(10.4).d(-5832.5).nl()
.d("EARTH").d(5.97).d(12756).d(5514).d(9.8).d(11.2).d(23.9).nl()
.d("MOON").d(0.0073).d(3475).d(3340).d(1.6).d(2.4).d(655.7).nl()
.d("MARS").d(0.642).d(6792).d(3933).d(3.7).d(5.0).d(24.6).nl()
.d("JUPITER").d(1898).d(142984).d(1326).d(23.1).d(59.5).d(9.9).nl()
.d("SATURN").d(568).d(120536).d(687).d(9.0).d(35.5).d(10.7).nl()
.d("URANUS").d(86.8).d(51118).d(1271).d(8.7).d(21.3).d(-17.2).nl()
.d("NEPTUNE").d(102).d(49528).d(1638).d(11.0).d(23.5).d(16.1).nl()
.d("PLUTO").d(0.0146).d(2370).d(2095).d(0.7).d(1.3).d(-153.3).nl()
.toString());
```

outputs:

```
$ node sample/planets.js
NAME Mass(10^24kg) Dia(km) Dens(kg/m3) Grav(m/s2) EscV(km/s) Rot(hours)
MERCURY 0.33 4879 5427 3.7 4.3 1407.6
VENUS 4.87 12104 5243 8.9 10.4 -5832.5
EARTH 5.97 12756 5514 9.8 11.2 23.9
MOON 0.0073 3475 3340 1.6 2.4 655.7
MARS 0.642 6792 3933 3.7 5.0 24.6
JUPITER 1898.0 142984 1326 23.1 59.5 9.9
SATURN 568.0 120536 687 9.0 35.5 10.7
URANUS 86.8 51118 1271 8.7 21.3 -17.2
NEPTUNE 102.0 49528 1638 11.0 23.5 16.1
PLUTO 0.0146 2370 2095 0.7 1.3 -153.3
```


METHODS
-------

### buffer(opt)

Creates a `ListItBuffer` instance and returns it.

The instance has current row that is a position for the columns to be added.

You cannot edit the columns and rows that were already added.

See the examples below.

#### opt.autoAlign

When this is set true, the data in cell will be aligned in automatic depending on its type.

The number will be aligned to the right taking account of its decimal point.

* Type : boolean
* Default setting : false

### ListItBuffer.d(string)

Adds a new column to the current row.
The `d` is representing 'data'.

Returns `this` object. So you can chain a method call.

### ListItBuffer.nl()

Ends up a process for the current row.

Returns `this` object.

### ListItBuffer.toString()

Returns preformatted text table.


LICENCE
-------
Expand Down
110 changes: 102 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,46 @@
var exports = {};
var privates = {};

exports.buffer = function() {
return new privates.ListItBuffer();
exports.buffer = function(opt) {
return new privates.ListItBuffer(opt);
};

privates.ListItBuffer = function() {
privates.ListItBuffer = function(opt) {
this.opt = {
"autoAlign" : false
};
if(opt) {
Object.keys(this.opt).forEach(function(key) {
if(key in opt) {
this.opt[key] = opt[key];
}
}, this);
}
this.lines = [];
this.colmaxlen = [];
};
privates.ListItBuffer.prototype.nl = function() {
this.lines.push(new Row());
return this;
};
privates.ListItBuffer.prototype.d = function(s) {
privates.ListItBuffer.prototype.d = function(data) {
if(this.lines.length <= 0) {
this.nl();
}
var row = this.lines.length - 1;
var col = this.lines[row].getCellLength();
if(col >= this.colmaxlen.length) {
var column = new Column();
if(this.opt.autoAlign) {
column.setAutoAlign(true);
}
this.colmaxlen.push(column);
col = this.colmaxlen.length - 1;
}
this.colmaxlen[col].expandWidth(s.length);
var cell = new DataCell();
cell.setData(s);
cell.setData(data);
this.lines[row].pushCell(cell);
this.colmaxlen[col].setCellAt(row, cell);
return this;
};
privates.ListItBuffer.prototype.toString = function() {
Expand Down Expand Up @@ -66,24 +79,105 @@
// Column class
//
var Column = function() {
this.opt = {
"autoAlign" : false
};
this.width = 0;
this.intLen = 0;
this.fracLen = 0;
this.cellAtRow = {};
};
Column.prototype.getWidth = function() {
return this.width;
};
Column.prototype.expandWidth = function(width) {
Column.prototype.setCellAt = function(row, cell) {
var data = cell.getData();
var s = "" + data;
var width = s.length;
this.cellAtRow[row] = cell;
if(this.opt.autoAlign) {
if(typeof(data) == "number") {
this.updateNumWidth(data);
width = this.getNumMaxWidth();
}
}
if(width > this.width) {
this.width = width;
}
};
Column.prototype.formatCell = function(cell) {
var m = this.getWidth();
var s = cell.getData();
var data = cell.getData();
if(this.opt.autoAlign) {
if(typeof(data) == "number") {
var s = this.makeAutoAlignNum(data);
while(s.length < m) {
s = ' ' + s;
}
return s;
}
}
var s = "" + data;
while(s.length < m) {
s += ' ';
}
return s;
};
Column.prototype.setAutoAlign = function(autoAlign) {
this.opt.autoAlign = autoAlign;
};
Column.prototype.updateNumWidth = function(num) {
var numInfo = this.analyzeNumber(num);
var intLen = numInfo.intStr.length;
var fracLen = numInfo.fracStr.length;
if(intLen > this.intLen) {
this.intLen = intLen;
}
if(fracLen > this.fracLen) {
this.fracLen = fracLen;
}
};
Column.prototype.getNumMaxWidth = function() {
var width = this.intLen + this.fracLen;
if(this.fracLen > 0) {
width++;
}
return width;
};
Column.prototype.makeAutoAlignNum = function(num) {
var s = "";
var numInfo = this.analyzeNumber(num);
var intStr = numInfo.intStr;
var fracStr = numInfo.fracStr;
var pointPos = numInfo.pointPos;
while(intStr.length < this.intLen) {
intStr = " " + intStr;
}
while(fracStr.length < this.fracLen) {
fracStr = fracStr + " ";
}
if(pointPos >= 0) {
s = intStr + "." + fracStr;
} else if(this.fracLen == 0) {
s = intStr;
} else {
s = intStr + ".0" + fracStr.substr(1);
}
return s;
};
Column.prototype.analyzeNumber = function(num) {
var s = "" + num;
var intStr = "";
var fracStr = "";
var pointPos = s.indexOf('.');
if(pointPos < 0) {
intStr = s;
} else {
intStr = s.substr(0, pointPos);
fracStr = s.substr(pointPos + 1);
}
return { "pointPos" : pointPos, "intStr" : intStr, "fracStr" : fracStr };
};

//
// Row class
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "list-it",
"version": "0.1.0",
"version": "0.2.0",
"description":
"This module is used to create a preformatted text table.",
"repository": {
Expand Down
18 changes: 18 additions & 0 deletions sample/planets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var listit = require("../lib");
var buf = listit.buffer({ "autoAlign" : true });
console.log(
buf
.d("NAME").d("Mass(10^24kg)").d("Dia(km)")
.d("Dens(kg/m3)").d("Grav(m/s2)")
.d("EscV(km/s)").d("Rot(hours)").nl()
.d("MERCURY").d(0.33).d(4879).d(5427).d(3.7).d(4.3).d(1407.6).nl()
.d("VENUS").d(4.87).d(12104).d(5243).d(8.9).d(10.4).d(-5832.5).nl()
.d("EARTH").d(5.97).d(12756).d(5514).d(9.8).d(11.2).d(23.9).nl()
.d("MOON").d(0.0073).d(3475).d(3340).d(1.6).d(2.4).d(655.7).nl()
.d("MARS").d(0.642).d(6792).d(3933).d(3.7).d(5.0).d(24.6).nl()
.d("JUPITER").d(1898).d(142984).d(1326).d(23.1).d(59.5).d(9.9).nl()
.d("SATURN").d(568).d(120536).d(687).d(9.0).d(35.5).d(10.7).nl()
.d("URANUS").d(86.8).d(51118).d(1271).d(8.7).d(21.3).d(-17.2).nl()
.d("NEPTUNE").d(102).d(49528).d(1638).d(11.0).d(23.5).d(16.1).nl()
.d("PLUTO").d(0.0146).d(2370).d(2095).d(0.7).d(1.3).d(-153.3).nl()
.toString());
Loading

0 comments on commit 49ac20d

Please sign in to comment.