Skip to content

Commit b5e016b

Browse files
committed
Merge pull request #10 from takamin/add-by-array
Allow to put several columns or rows at a time
2 parents 49ac20d + 9f3b42b commit b5e016b

File tree

5 files changed

+247
-51
lines changed

5 files changed

+247
-51
lines changed

README.md

+63-21
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,21 @@ __planets.js__
5858
```
5959
var listit = require("list-it");
6060
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());
61+
var PLANETS = [
62+
["NAME", "Mass(10^24kg)", "Dia(km)", "Dens(kg/m3)",
63+
"Grav(m/s2)", "EscV(km/s)", "Rot(hours)" ],
64+
["MERCURY", 0.33, 4879, 5427, 3.7, 4.3, 1407.6 ],
65+
["VENUS", 4.87, 12104, 5243, 8.9, 10.4, -5832.5 ],
66+
["EARTH", 5.97, 12756, 5514, 9.8, 11.2, 23.9 ],
67+
["MOON", 0.0073, 3475, 3340, 1.6, 2.4, 655.7 ],
68+
["MARS", 0.642, 6792, 3933, 3.7, 5.0, 24.6 ],
69+
["JUPITER", 1898, 142984, 1326, 23.1, 59.5, 9.9 ],
70+
["SATURN", 568, 120536, 687, 9.0, 35.5, 10.7 ],
71+
["URANUS", 86.8, 51118, 1271, 8.7, 21.3, -17.2 ],
72+
["NEPTUNE", 102, 49528, 1638, 11.0, 23.5, 16.1 ],
73+
["PLUTO", 0.0146, 2370, 2095, 0.7, 1.3, -153.3 ]
74+
];
75+
console.log( buf.d( PLANETS ).toString() );
7776
```
7877

7978
outputs:
@@ -116,18 +115,61 @@ The number will be aligned to the right taking account of its decimal point.
116115
* Type : boolean
117116
* Default setting : false
118117

119-
### ListItBuffer.d(string)
118+
### ListItBuffer.d( data [, data ...] )
119+
120+
This method adds one or more columns or rows at a time depending on
121+
the parameter data type.
122+
123+
This method returns `this` object. So you can chain a method call.
124+
125+
#### How to add column(s)
126+
127+
If the type of `data` is a primitive type such as string or number,
128+
these are added to the current row as individual columns.
129+
130+
This operation will not add a new line in automatic.
131+
A code of below outputs only one row containing six column from 1 to 6.
132+
133+
```
134+
CODE: buffer.d(1,2,3).d(4,5,6).nl();
135+
136+
OUTPUT: "1 2 3 4 5 6"
137+
```
138+
139+
The above code make same result as below:
120140

121-
Adds a new column to the current row.
122-
The `d` is representing 'data'.
141+
```
142+
EQUIVALENT CODE: buffer.d(1,2,3,4,5,6).nl();
143+
```
144+
145+
#### How to add row(s)
146+
147+
If the parameter `data` is an array contains one or more primitive data at least,
148+
it will added as one closed row.
149+
150+
But if the type of all elements of the array is an array,
151+
in other words it is two or more dimensional array,
152+
each elements are added as a row.
153+
154+
NOTE: A new-line will never be added before the addition.
155+
So, If the previous row was not closed, you must call the `nl()` method.
156+
157+
The following sample outputs two rows:
158+
159+
```
160+
CODE: buffer.d( [1,2,3], [4,5,6] );
161+
162+
OUTPUT:"1 2 3\n4 5 6"
163+
164+
EQUIVALENT CODE: buffer.d([ [1,2,3], [4,5,6] ]);
165+
```
123166

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

126168
### ListItBuffer.nl()
127169

128170
Ends up a process for the current row.
129171

130-
Returns `this` object.
172+
This method also returns `this` object.
131173

132174
### ListItBuffer.toString()
133175

lib/index.js

+83-12
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,39 @@
2222
this.colmaxlen = [];
2323
};
2424
privates.ListItBuffer.prototype.nl = function() {
25-
this.lines.push(new Row());
25+
var lastRowIndex = this.lines.length - 1;
26+
if(lastRowIndex >= 0 && !this.lines[lastRowIndex].isFixed()) {
27+
this.lines[lastRowIndex].fix();
28+
}
29+
this.pushNewRow();
2630
return this;
2731
};
2832
privates.ListItBuffer.prototype.d = function(data) {
29-
if(this.lines.length <= 0) {
30-
this.nl();
33+
if(arguments.length > 1) {
34+
var array = Array.apply(null, arguments);
35+
if(privates.ListItBuffer.isMultiDimensionalArray(array)) {
36+
array.forEach(function(row) {
37+
this.addRow(row);
38+
}, this);
39+
} else {
40+
array.forEach(function(data) {
41+
this.d(data);
42+
}, this);
43+
}
44+
return this;
45+
} else if(Array.isArray(data)) {
46+
if(privates.ListItBuffer.isMultiDimensionalArray(data)) {
47+
data.forEach(function(row) {
48+
this.addRow(row);
49+
}, this);
50+
} else {
51+
this.addRow(data);
52+
}
53+
return this;
54+
}
55+
var rowidx = this.lines.length - 1;
56+
if(rowidx < 0 || this.lines[rowidx].isFixed()) {
57+
this.pushNewRow();
3158
}
3259
var row = this.lines.length - 1;
3360
var col = this.lines[row].getCellLength();
@@ -45,20 +72,49 @@
4572
this.colmaxlen[col].setCellAt(row, cell);
4673
return this;
4774
};
75+
privates.ListItBuffer.isMultiDimensionalArray = function(arr) {
76+
var allElementIsArray = true;
77+
arr.forEach(function(element) {
78+
if(!Array.isArray(element)) {
79+
allElementIsArray = false;
80+
}
81+
});
82+
return allElementIsArray;
83+
};
84+
privates.ListItBuffer.prototype.addRow = function(row) {
85+
row.forEach(function(col) {
86+
switch(typeof(col)) {
87+
case 'string':
88+
case 'number':
89+
this.d(col);
90+
break;
91+
default:
92+
this.d(col.toString());
93+
break;
94+
}
95+
}, this);
96+
this.nl();
97+
};
98+
privates.ListItBuffer.prototype.pushNewRow = function() {
99+
this.lines.push(new Row());
100+
};
48101
privates.ListItBuffer.prototype.toString = function() {
49-
if(this.lines.length <= 0) {
50-
return "";
51-
}
52102
var rows = [];
53103
this.lines.forEach(function(line) {
54-
var cols = [];
55-
for(var col = 0; col < line.getCellLength(); col++) {
56-
var s = this.colmaxlen[col].formatCell(line.getCell(col));
57-
cols.push(s);
104+
if(line.isFixed() || !line.isEmpty()) {
105+
if(line.isEmpty()) {
106+
rows.push('\n');
107+
} else {
108+
var cols = [];
109+
for(var col = 0; col < line.getCellLength(); col++) {
110+
var s = this.colmaxlen[col].formatCell(line.getCell(col));
111+
cols.push(s);
112+
}
113+
rows.push(cols.join(' '));
114+
}
58115
}
59-
rows.push(cols.join(' '));
60116
}, this);
61-
return rows.join("\n");;
117+
return rows.join("\n");
62118
};
63119
module.exports = exports;
64120

@@ -184,6 +240,8 @@
184240
//
185241
var Row = function() {
186242
this.cells = [];
243+
this.empty = true;
244+
this.fixed = false;
187245
};
188246
Row.prototype.getCellLength = function() {
189247
return this.cells.length;
@@ -192,6 +250,19 @@
192250
return this.cells[idx];
193251
};
194252
Row.prototype.pushCell = function(cell) {
253+
if(this.isFixed()) {
254+
throw "pushCell Fail, The row was already fixed.";
255+
}
256+
this.empty = false;
195257
return this.cells.push(cell);
196258
};
259+
Row.prototype.fix = function() {
260+
this.fixed = true;
261+
};
262+
Row.prototype.isEmpty = function() {
263+
return this.empty;
264+
};
265+
Row.prototype.isFixed = function() {
266+
return this.fixed;
267+
};
197268
}());

package.json

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

sample/planets.js

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

test/test.js

+85-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
buffer.nl();
6868
buffer.d("DDDD").d("EEEEE").d("FFFFFF");
6969
assert.equal(buffer.toString(),
70-
"A BB CCC \n" +
70+
"A BB CCC \n" +
7171
"DDDD EEEEE FFFFFF");
7272
});
7373
});
@@ -163,4 +163,88 @@
163163
});
164164
});
165165
});
166+
describe("put several data at a time", function() {
167+
describe("multi-columns, no adding new-line", function() {
168+
it("no autoAlign", function() {
169+
var buffer = new listit.buffer();
170+
buffer.d("A", "BB").d("CCC","DDDD").nl();
171+
buffer.d("EEEEE").d("FFFFFF", "GGGGGGG", "HHHHHHHH");
172+
assert.equal(buffer.toString(),
173+
"A BB CCC DDDD \n" +
174+
"EEEEE FFFFFF GGGGGGG HHHHHHHH");
175+
});
176+
it("autoAlign", function() {
177+
var buffer = new listit.buffer({autoAlign:true});
178+
buffer.d("A", 11).d("B", 2222).nl();
179+
buffer.d("C", 333.3).d("D", -4.444);
180+
assert.equal(buffer.toString(),
181+
"A 11.0 B 2222.0 \n" +
182+
"C 333.3 D -4.444");
183+
});
184+
});
185+
describe("one row", function() {
186+
it("no autoAlign", function() {
187+
var buffer = new listit.buffer();
188+
buffer.d([ "A", "BB", "CCC","DDDD" ]);
189+
buffer.d(["EEEEE", "FFFFFF", "GGGGGGG", "HHHHHHHH"]);
190+
assert.equal(buffer.toString(),
191+
"A BB CCC DDDD \n" +
192+
"EEEEE FFFFFF GGGGGGG HHHHHHHH");
193+
});
194+
it("autoAlign", function() {
195+
var buffer = new listit.buffer({autoAlign:true});
196+
buffer.d([ "A", 11, "B", 2222]);
197+
buffer.d([ "C", 333.3, "D", -4.444]);
198+
assert.equal(buffer.toString(),
199+
"A 11.0 B 2222.0 \n" +
200+
"C 333.3 D -4.444");
201+
});
202+
});
203+
describe("multi row", function() {
204+
describe("all arguments are array", function() {
205+
it("no autoAlign", function() {
206+
var buffer = new listit.buffer();
207+
buffer.d(
208+
[ "A", "BB", "CCC","DDDD" ],
209+
["EEEEE", "FFFFFF", "GGGGGGG", "HHHHHHHH"]
210+
);
211+
assert.equal(buffer.toString(),
212+
"A BB CCC DDDD \n" +
213+
"EEEEE FFFFFF GGGGGGG HHHHHHHH");
214+
});
215+
it("autoAlign", function() {
216+
var buffer = new listit.buffer({autoAlign:true});
217+
buffer.d(
218+
[ "A", 11, "B", 2222],
219+
[ "C", 333.3, "D", -4.444]
220+
);
221+
assert.equal(buffer.toString(),
222+
"A 11.0 B 2222.0 \n" +
223+
"C 333.3 D -4.444");
224+
});
225+
});
226+
describe("bidimentional data", function() {
227+
it("no autoAlign", function() {
228+
var buffer = new listit.buffer();
229+
buffer.d([
230+
[ "A", "BB", "CCC","DDDD" ],
231+
["EEEEE", "FFFFFF", "GGGGGGG", "HHHHHHHH"]
232+
]);
233+
assert.equal(buffer.toString(),
234+
"A BB CCC DDDD \n" +
235+
"EEEEE FFFFFF GGGGGGG HHHHHHHH");
236+
});
237+
it("autoAlign", function() {
238+
var buffer = new listit.buffer({autoAlign:true});
239+
buffer.d([
240+
[ "A", 11, "B", 2222],
241+
[ "C", 333.3, "D", -4.444]
242+
]);
243+
assert.equal(buffer.toString(),
244+
"A 11.0 B 2222.0 \n" +
245+
"C 333.3 D -4.444");
246+
});
247+
});
248+
});
249+
});
166250
}());

0 commit comments

Comments
 (0)