Skip to content

Commit

Permalink
Merge pull request #10 from takamin/add-by-array
Browse files Browse the repository at this point in the history
Allow to put several columns or rows at a time
takamin committed Apr 25, 2016
2 parents 49ac20d + 9f3b42b commit b5e016b
Showing 5 changed files with 247 additions and 51 deletions.
84 changes: 63 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -58,22 +58,21 @@ __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());
var PLANETS = [
["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, 142984, 1326, 23.1, 59.5, 9.9 ],
["SATURN", 568, 120536, 687, 9.0, 35.5, 10.7 ],
["URANUS", 86.8, 51118, 1271, 8.7, 21.3, -17.2 ],
["NEPTUNE", 102, 49528, 1638, 11.0, 23.5, 16.1 ],
["PLUTO", 0.0146, 2370, 2095, 0.7, 1.3, -153.3 ]
];
console.log( buf.d( PLANETS ).toString() );
```

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

### ListItBuffer.d(string)
### ListItBuffer.d( data [, data ...] )

This method adds one or more columns or rows at a time depending on
the parameter data type.

This method returns `this` object. So you can chain a method call.

#### How to add column(s)

If the type of `data` is a primitive type such as string or number,
these are added to the current row as individual columns.

This operation will not add a new line in automatic.
A code of below outputs only one row containing six column from 1 to 6.

```
CODE: buffer.d(1,2,3).d(4,5,6).nl();
OUTPUT: "1 2 3 4 5 6"
```

The above code make same result as below:

Adds a new column to the current row.
The `d` is representing 'data'.
```
EQUIVALENT CODE: buffer.d(1,2,3,4,5,6).nl();
```

#### How to add row(s)

If the parameter `data` is an array contains one or more primitive data at least,
it will added as one closed row.

But if the type of all elements of the array is an array,
in other words it is two or more dimensional array,
each elements are added as a row.

NOTE: A new-line will never be added before the addition.
So, If the previous row was not closed, you must call the `nl()` method.

The following sample outputs two rows:

```
CODE: buffer.d( [1,2,3], [4,5,6] );
OUTPUT:"1 2 3\n4 5 6"
EQUIVALENT CODE: buffer.d([ [1,2,3], [4,5,6] ]);
```

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

### ListItBuffer.nl()

Ends up a process for the current row.

Returns `this` object.
This method also returns `this` object.

### ListItBuffer.toString()

95 changes: 83 additions & 12 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -22,12 +22,39 @@
this.colmaxlen = [];
};
privates.ListItBuffer.prototype.nl = function() {
this.lines.push(new Row());
var lastRowIndex = this.lines.length - 1;
if(lastRowIndex >= 0 && !this.lines[lastRowIndex].isFixed()) {
this.lines[lastRowIndex].fix();
}
this.pushNewRow();
return this;
};
privates.ListItBuffer.prototype.d = function(data) {
if(this.lines.length <= 0) {
this.nl();
if(arguments.length > 1) {
var array = Array.apply(null, arguments);
if(privates.ListItBuffer.isMultiDimensionalArray(array)) {
array.forEach(function(row) {
this.addRow(row);
}, this);
} else {
array.forEach(function(data) {
this.d(data);
}, this);
}
return this;
} else if(Array.isArray(data)) {
if(privates.ListItBuffer.isMultiDimensionalArray(data)) {
data.forEach(function(row) {
this.addRow(row);
}, this);
} else {
this.addRow(data);
}
return this;
}
var rowidx = this.lines.length - 1;
if(rowidx < 0 || this.lines[rowidx].isFixed()) {
this.pushNewRow();
}
var row = this.lines.length - 1;
var col = this.lines[row].getCellLength();
@@ -45,20 +72,49 @@
this.colmaxlen[col].setCellAt(row, cell);
return this;
};
privates.ListItBuffer.isMultiDimensionalArray = function(arr) {
var allElementIsArray = true;
arr.forEach(function(element) {
if(!Array.isArray(element)) {
allElementIsArray = false;
}
});
return allElementIsArray;
};
privates.ListItBuffer.prototype.addRow = function(row) {
row.forEach(function(col) {
switch(typeof(col)) {
case 'string':
case 'number':
this.d(col);
break;
default:
this.d(col.toString());
break;
}
}, this);
this.nl();
};
privates.ListItBuffer.prototype.pushNewRow = function() {
this.lines.push(new Row());
};
privates.ListItBuffer.prototype.toString = function() {
if(this.lines.length <= 0) {
return "";
}
var rows = [];
this.lines.forEach(function(line) {
var cols = [];
for(var col = 0; col < line.getCellLength(); col++) {
var s = this.colmaxlen[col].formatCell(line.getCell(col));
cols.push(s);
if(line.isFixed() || !line.isEmpty()) {
if(line.isEmpty()) {
rows.push('\n');
} else {
var cols = [];
for(var col = 0; col < line.getCellLength(); col++) {
var s = this.colmaxlen[col].formatCell(line.getCell(col));
cols.push(s);
}
rows.push(cols.join(' '));
}
}
rows.push(cols.join(' '));
}, this);
return rows.join("\n");;
return rows.join("\n");
};
module.exports = exports;

@@ -184,6 +240,8 @@
//
var Row = function() {
this.cells = [];
this.empty = true;
this.fixed = false;
};
Row.prototype.getCellLength = function() {
return this.cells.length;
@@ -192,6 +250,19 @@
return this.cells[idx];
};
Row.prototype.pushCell = function(cell) {
if(this.isFixed()) {
throw "pushCell Fail, The row was already fixed.";
}
this.empty = false;
return this.cells.push(cell);
};
Row.prototype.fix = function() {
this.fixed = true;
};
Row.prototype.isEmpty = function() {
return this.empty;
};
Row.prototype.isFixed = function() {
return this.fixed;
};
}());
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.2.0",
"version": "0.3.0",
"description":
"This module is used to create a preformatted text table.",
"repository": {
31 changes: 15 additions & 16 deletions sample/planets.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
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());
var PLANETS = [
["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, 142984, 1326, 23.1, 59.5, 9.9 ],
["SATURN", 568, 120536, 687, 9.0, 35.5, 10.7 ],
["URANUS", 86.8, 51118, 1271, 8.7, 21.3, -17.2 ],
["NEPTUNE", 102, 49528, 1638, 11.0, 23.5, 16.1 ],
["PLUTO", 0.0146, 2370, 2095, 0.7, 1.3, -153.3 ]
];
console.log( buf.d( PLANETS ).toString() );
86 changes: 85 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
@@ -67,7 +67,7 @@
buffer.nl();
buffer.d("DDDD").d("EEEEE").d("FFFFFF");
assert.equal(buffer.toString(),
"A BB CCC \n" +
"A BB CCC \n" +
"DDDD EEEEE FFFFFF");
});
});
@@ -163,4 +163,88 @@
});
});
});
describe("put several data at a time", function() {
describe("multi-columns, no adding new-line", function() {
it("no autoAlign", function() {
var buffer = new listit.buffer();
buffer.d("A", "BB").d("CCC","DDDD").nl();
buffer.d("EEEEE").d("FFFFFF", "GGGGGGG", "HHHHHHHH");
assert.equal(buffer.toString(),
"A BB CCC DDDD \n" +
"EEEEE FFFFFF GGGGGGG HHHHHHHH");
});
it("autoAlign", function() {
var buffer = new listit.buffer({autoAlign:true});
buffer.d("A", 11).d("B", 2222).nl();
buffer.d("C", 333.3).d("D", -4.444);
assert.equal(buffer.toString(),
"A 11.0 B 2222.0 \n" +
"C 333.3 D -4.444");
});
});
describe("one row", function() {
it("no autoAlign", function() {
var buffer = new listit.buffer();
buffer.d([ "A", "BB", "CCC","DDDD" ]);
buffer.d(["EEEEE", "FFFFFF", "GGGGGGG", "HHHHHHHH"]);
assert.equal(buffer.toString(),
"A BB CCC DDDD \n" +
"EEEEE FFFFFF GGGGGGG HHHHHHHH");
});
it("autoAlign", function() {
var buffer = new listit.buffer({autoAlign:true});
buffer.d([ "A", 11, "B", 2222]);
buffer.d([ "C", 333.3, "D", -4.444]);
assert.equal(buffer.toString(),
"A 11.0 B 2222.0 \n" +
"C 333.3 D -4.444");
});
});
describe("multi row", function() {
describe("all arguments are array", function() {
it("no autoAlign", function() {
var buffer = new listit.buffer();
buffer.d(
[ "A", "BB", "CCC","DDDD" ],
["EEEEE", "FFFFFF", "GGGGGGG", "HHHHHHHH"]
);
assert.equal(buffer.toString(),
"A BB CCC DDDD \n" +
"EEEEE FFFFFF GGGGGGG HHHHHHHH");
});
it("autoAlign", function() {
var buffer = new listit.buffer({autoAlign:true});
buffer.d(
[ "A", 11, "B", 2222],
[ "C", 333.3, "D", -4.444]
);
assert.equal(buffer.toString(),
"A 11.0 B 2222.0 \n" +
"C 333.3 D -4.444");
});
});
describe("bidimentional data", function() {
it("no autoAlign", function() {
var buffer = new listit.buffer();
buffer.d([
[ "A", "BB", "CCC","DDDD" ],
["EEEEE", "FFFFFF", "GGGGGGG", "HHHHHHHH"]
]);
assert.equal(buffer.toString(),
"A BB CCC DDDD \n" +
"EEEEE FFFFFF GGGGGGG HHHHHHHH");
});
it("autoAlign", function() {
var buffer = new listit.buffer({autoAlign:true});
buffer.d([
[ "A", 11, "B", 2222],
[ "C", 333.3, "D", -4.444]
]);
assert.equal(buffer.toString(),
"A 11.0 B 2222.0 \n" +
"C 333.3 D -4.444");
});
});
});
});
}());

0 comments on commit b5e016b

Please sign in to comment.