Skip to content

Commit 49ac20d

Browse files
committed
Merge pull request #8 from takamin/issue4-autoAlign
issue4: The autoAlign mode is available
2 parents 7f920f9 + eae9411 commit 49ac20d

File tree

5 files changed

+295
-41
lines changed

5 files changed

+295
-41
lines changed

README.md

+83-32
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,7 @@ Each columns in all rows are aligned in vertical.
1010

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

13-
METHODS
14-
-------
15-
16-
### buffer()
17-
18-
Creates a `ListItBuffer` instance and returns it.
19-
20-
The instance has current row that is a position for the columns to be added.
21-
22-
You cannot edit the columns and rows that were already added.
23-
24-
See the examples below.
25-
26-
### ListItBuffer.d(string)
27-
28-
Adds a new column to the current row.
29-
The `d` is representing 'data'.
30-
31-
Returns `this` object. So you can chain a method call.
32-
33-
### ListItBuffer.nl()
34-
35-
Ends up a process for the current row.
36-
37-
Returns `this` object.
38-
39-
### ListItBuffer.toString()
40-
41-
Returns preformatted text table.
42-
13+
When a autoAlign option is set, the numbers are aligned by its fraction point.
4314

4415
SAMPLE
4516
------
@@ -80,8 +51,88 @@ $ node sample/japanese-food.js
8051
5 Sashimi Very fresh sliced fish Try it now, It's good
8152
```
8253

83-
METHOD
84-
------
54+
### autoAlign
55+
56+
__planets.js__
57+
58+
```
59+
var listit = require("list-it");
60+
var buf = listit.buffer({ "autoAlign" : true });
61+
console.log(
62+
buf
63+
.d("NAME").d("Mass(10^24kg)").d("Dia(km)")
64+
.d("Dens(kg/m3)").d("Grav(m/s2)")
65+
.d("EscV(km/s)").d("Rot(hours)").nl()
66+
.d("MERCURY").d(0.33).d(4879).d(5427).d(3.7).d(4.3).d(1407.6).nl()
67+
.d("VENUS").d(4.87).d(12104).d(5243).d(8.9).d(10.4).d(-5832.5).nl()
68+
.d("EARTH").d(5.97).d(12756).d(5514).d(9.8).d(11.2).d(23.9).nl()
69+
.d("MOON").d(0.0073).d(3475).d(3340).d(1.6).d(2.4).d(655.7).nl()
70+
.d("MARS").d(0.642).d(6792).d(3933).d(3.7).d(5.0).d(24.6).nl()
71+
.d("JUPITER").d(1898).d(142984).d(1326).d(23.1).d(59.5).d(9.9).nl()
72+
.d("SATURN").d(568).d(120536).d(687).d(9.0).d(35.5).d(10.7).nl()
73+
.d("URANUS").d(86.8).d(51118).d(1271).d(8.7).d(21.3).d(-17.2).nl()
74+
.d("NEPTUNE").d(102).d(49528).d(1638).d(11.0).d(23.5).d(16.1).nl()
75+
.d("PLUTO").d(0.0146).d(2370).d(2095).d(0.7).d(1.3).d(-153.3).nl()
76+
.toString());
77+
```
78+
79+
outputs:
80+
81+
```
82+
$ node sample/planets.js
83+
NAME Mass(10^24kg) Dia(km) Dens(kg/m3) Grav(m/s2) EscV(km/s) Rot(hours)
84+
MERCURY 0.33 4879 5427 3.7 4.3 1407.6
85+
VENUS 4.87 12104 5243 8.9 10.4 -5832.5
86+
EARTH 5.97 12756 5514 9.8 11.2 23.9
87+
MOON 0.0073 3475 3340 1.6 2.4 655.7
88+
MARS 0.642 6792 3933 3.7 5.0 24.6
89+
JUPITER 1898.0 142984 1326 23.1 59.5 9.9
90+
SATURN 568.0 120536 687 9.0 35.5 10.7
91+
URANUS 86.8 51118 1271 8.7 21.3 -17.2
92+
NEPTUNE 102.0 49528 1638 11.0 23.5 16.1
93+
PLUTO 0.0146 2370 2095 0.7 1.3 -153.3
94+
```
95+
96+
97+
METHODS
98+
-------
99+
100+
### buffer(opt)
101+
102+
Creates a `ListItBuffer` instance and returns it.
103+
104+
The instance has current row that is a position for the columns to be added.
105+
106+
You cannot edit the columns and rows that were already added.
107+
108+
See the examples below.
109+
110+
#### opt.autoAlign
111+
112+
When this is set true, the data in cell will be aligned in automatic depending on its type.
113+
114+
The number will be aligned to the right taking account of its decimal point.
115+
116+
* Type : boolean
117+
* Default setting : false
118+
119+
### ListItBuffer.d(string)
120+
121+
Adds a new column to the current row.
122+
The `d` is representing 'data'.
123+
124+
Returns `this` object. So you can chain a method call.
125+
126+
### ListItBuffer.nl()
127+
128+
Ends up a process for the current row.
129+
130+
Returns `this` object.
131+
132+
### ListItBuffer.toString()
133+
134+
Returns preformatted text table.
135+
85136

86137
LICENCE
87138
-------

lib/index.js

+102-8
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,46 @@
33
var exports = {};
44
var privates = {};
55

6-
exports.buffer = function() {
7-
return new privates.ListItBuffer();
6+
exports.buffer = function(opt) {
7+
return new privates.ListItBuffer(opt);
88
};
99

10-
privates.ListItBuffer = function() {
10+
privates.ListItBuffer = function(opt) {
11+
this.opt = {
12+
"autoAlign" : false
13+
};
14+
if(opt) {
15+
Object.keys(this.opt).forEach(function(key) {
16+
if(key in opt) {
17+
this.opt[key] = opt[key];
18+
}
19+
}, this);
20+
}
1121
this.lines = [];
1222
this.colmaxlen = [];
1323
};
1424
privates.ListItBuffer.prototype.nl = function() {
1525
this.lines.push(new Row());
1626
return this;
1727
};
18-
privates.ListItBuffer.prototype.d = function(s) {
28+
privates.ListItBuffer.prototype.d = function(data) {
1929
if(this.lines.length <= 0) {
2030
this.nl();
2131
}
2232
var row = this.lines.length - 1;
2333
var col = this.lines[row].getCellLength();
2434
if(col >= this.colmaxlen.length) {
2535
var column = new Column();
36+
if(this.opt.autoAlign) {
37+
column.setAutoAlign(true);
38+
}
2639
this.colmaxlen.push(column);
2740
col = this.colmaxlen.length - 1;
2841
}
29-
this.colmaxlen[col].expandWidth(s.length);
3042
var cell = new DataCell();
31-
cell.setData(s);
43+
cell.setData(data);
3244
this.lines[row].pushCell(cell);
45+
this.colmaxlen[col].setCellAt(row, cell);
3346
return this;
3447
};
3548
privates.ListItBuffer.prototype.toString = function() {
@@ -66,24 +79,105 @@
6679
// Column class
6780
//
6881
var Column = function() {
82+
this.opt = {
83+
"autoAlign" : false
84+
};
6985
this.width = 0;
86+
this.intLen = 0;
87+
this.fracLen = 0;
88+
this.cellAtRow = {};
7089
};
7190
Column.prototype.getWidth = function() {
7291
return this.width;
7392
};
74-
Column.prototype.expandWidth = function(width) {
93+
Column.prototype.setCellAt = function(row, cell) {
94+
var data = cell.getData();
95+
var s = "" + data;
96+
var width = s.length;
97+
this.cellAtRow[row] = cell;
98+
if(this.opt.autoAlign) {
99+
if(typeof(data) == "number") {
100+
this.updateNumWidth(data);
101+
width = this.getNumMaxWidth();
102+
}
103+
}
75104
if(width > this.width) {
76105
this.width = width;
77106
}
78107
};
79108
Column.prototype.formatCell = function(cell) {
80109
var m = this.getWidth();
81-
var s = cell.getData();
110+
var data = cell.getData();
111+
if(this.opt.autoAlign) {
112+
if(typeof(data) == "number") {
113+
var s = this.makeAutoAlignNum(data);
114+
while(s.length < m) {
115+
s = ' ' + s;
116+
}
117+
return s;
118+
}
119+
}
120+
var s = "" + data;
82121
while(s.length < m) {
83122
s += ' ';
84123
}
85124
return s;
86125
};
126+
Column.prototype.setAutoAlign = function(autoAlign) {
127+
this.opt.autoAlign = autoAlign;
128+
};
129+
Column.prototype.updateNumWidth = function(num) {
130+
var numInfo = this.analyzeNumber(num);
131+
var intLen = numInfo.intStr.length;
132+
var fracLen = numInfo.fracStr.length;
133+
if(intLen > this.intLen) {
134+
this.intLen = intLen;
135+
}
136+
if(fracLen > this.fracLen) {
137+
this.fracLen = fracLen;
138+
}
139+
};
140+
Column.prototype.getNumMaxWidth = function() {
141+
var width = this.intLen + this.fracLen;
142+
if(this.fracLen > 0) {
143+
width++;
144+
}
145+
return width;
146+
};
147+
Column.prototype.makeAutoAlignNum = function(num) {
148+
var s = "";
149+
var numInfo = this.analyzeNumber(num);
150+
var intStr = numInfo.intStr;
151+
var fracStr = numInfo.fracStr;
152+
var pointPos = numInfo.pointPos;
153+
while(intStr.length < this.intLen) {
154+
intStr = " " + intStr;
155+
}
156+
while(fracStr.length < this.fracLen) {
157+
fracStr = fracStr + " ";
158+
}
159+
if(pointPos >= 0) {
160+
s = intStr + "." + fracStr;
161+
} else if(this.fracLen == 0) {
162+
s = intStr;
163+
} else {
164+
s = intStr + ".0" + fracStr.substr(1);
165+
}
166+
return s;
167+
};
168+
Column.prototype.analyzeNumber = function(num) {
169+
var s = "" + num;
170+
var intStr = "";
171+
var fracStr = "";
172+
var pointPos = s.indexOf('.');
173+
if(pointPos < 0) {
174+
intStr = s;
175+
} else {
176+
intStr = s.substr(0, pointPos);
177+
fracStr = s.substr(pointPos + 1);
178+
}
179+
return { "pointPos" : pointPos, "intStr" : intStr, "fracStr" : fracStr };
180+
};
87181

88182
//
89183
// Row class

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "list-it",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"description":
55
"This module is used to create a preformatted text table.",
66
"repository": {

sample/planets.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var listit = require("../lib");
2+
var buf = listit.buffer({ "autoAlign" : true });
3+
console.log(
4+
buf
5+
.d("NAME").d("Mass(10^24kg)").d("Dia(km)")
6+
.d("Dens(kg/m3)").d("Grav(m/s2)")
7+
.d("EscV(km/s)").d("Rot(hours)").nl()
8+
.d("MERCURY").d(0.33).d(4879).d(5427).d(3.7).d(4.3).d(1407.6).nl()
9+
.d("VENUS").d(4.87).d(12104).d(5243).d(8.9).d(10.4).d(-5832.5).nl()
10+
.d("EARTH").d(5.97).d(12756).d(5514).d(9.8).d(11.2).d(23.9).nl()
11+
.d("MOON").d(0.0073).d(3475).d(3340).d(1.6).d(2.4).d(655.7).nl()
12+
.d("MARS").d(0.642).d(6792).d(3933).d(3.7).d(5.0).d(24.6).nl()
13+
.d("JUPITER").d(1898).d(142984).d(1326).d(23.1).d(59.5).d(9.9).nl()
14+
.d("SATURN").d(568).d(120536).d(687).d(9.0).d(35.5).d(10.7).nl()
15+
.d("URANUS").d(86.8).d(51118).d(1271).d(8.7).d(21.3).d(-17.2).nl()
16+
.d("NEPTUNE").d(102).d(49528).d(1638).d(11.0).d(23.5).d(16.1).nl()
17+
.d("PLUTO").d(0.0146).d(2370).d(2095).d(0.7).d(1.3).d(-153.3).nl()
18+
.toString());

0 commit comments

Comments
 (0)