Skip to content

Commit 7f920f9

Browse files
committed
Declare internal classes to represent a table elements
Following classes are added to make the easy to enhance in future. * DataCell - table data * Column - manage column attributes * Row - manage cells These are not exported. So, the public interfaces and its results are not changed at all.
1 parent e6dbed3 commit 7f920f9

File tree

1 file changed

+63
-12
lines changed

1 file changed

+63
-12
lines changed

lib/index.js

+63-12
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,24 @@
1212
this.colmaxlen = [];
1313
};
1414
privates.ListItBuffer.prototype.nl = function() {
15-
this.lines.push([]);
15+
this.lines.push(new Row());
1616
return this;
1717
};
1818
privates.ListItBuffer.prototype.d = function(s) {
1919
if(this.lines.length <= 0) {
2020
this.nl();
2121
}
2222
var row = this.lines.length - 1;
23-
var col = this.lines[row].length;
23+
var col = this.lines[row].getCellLength();
2424
if(col >= this.colmaxlen.length) {
25-
this.colmaxlen.push(s.length);
26-
} else if(this.colmaxlen[col] < s.length) {
27-
this.colmaxlen[col] = s.length;
25+
var column = new Column();
26+
this.colmaxlen.push(column);
27+
col = this.colmaxlen.length - 1;
2828
}
29-
this.lines[row].push(s);
29+
this.colmaxlen[col].expandWidth(s.length);
30+
var cell = new DataCell();
31+
cell.setData(s);
32+
this.lines[row].pushCell(cell);
3033
return this;
3134
};
3235
privates.ListItBuffer.prototype.toString = function() {
@@ -36,17 +39,65 @@
3639
var rows = [];
3740
this.lines.forEach(function(line) {
3841
var cols = [];
39-
for(var col = 0; col < line.length; col++) {
40-
var m = this.colmaxlen[col];
41-
var s = line[col];
42-
while(s.length < m) {
43-
s += ' ';
44-
}
42+
for(var col = 0; col < line.getCellLength(); col++) {
43+
var s = this.colmaxlen[col].formatCell(line.getCell(col));
4544
cols.push(s);
4645
}
4746
rows.push(cols.join(' '));
4847
}, this);
4948
return rows.join("\n");;
5049
};
5150
module.exports = exports;
51+
52+
//
53+
// DataCell class
54+
//
55+
var DataCell = function() {
56+
this.data = "";
57+
};
58+
DataCell.prototype.getData = function() {
59+
return this.data;
60+
};
61+
DataCell.prototype.setData = function(data) {
62+
this.data = data;
63+
};
64+
65+
//
66+
// Column class
67+
//
68+
var Column = function() {
69+
this.width = 0;
70+
};
71+
Column.prototype.getWidth = function() {
72+
return this.width;
73+
};
74+
Column.prototype.expandWidth = function(width) {
75+
if(width > this.width) {
76+
this.width = width;
77+
}
78+
};
79+
Column.prototype.formatCell = function(cell) {
80+
var m = this.getWidth();
81+
var s = cell.getData();
82+
while(s.length < m) {
83+
s += ' ';
84+
}
85+
return s;
86+
};
87+
88+
//
89+
// Row class
90+
//
91+
var Row = function() {
92+
this.cells = [];
93+
};
94+
Row.prototype.getCellLength = function() {
95+
return this.cells.length;
96+
};
97+
Row.prototype.getCell= function(idx) {
98+
return this.cells[idx];
99+
};
100+
Row.prototype.pushCell = function(cell) {
101+
return this.cells.push(cell);
102+
};
52103
}());

0 commit comments

Comments
 (0)