Skip to content

Commit 81834ab

Browse files
authored
Merge pull request #28 from takamin/column-width-option
Add columnWidth option and setColumnWidthAll method
2 parents 7d86711 + f4d6627 commit 81834ab

File tree

4 files changed

+250
-7
lines changed

4 files changed

+250
-7
lines changed

README.md

+36-1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,24 @@ The number will be aligned to the right taking account of its decimal point.
205205
* Type : boolean
206206
* Default setting : true
207207

208+
#### opt.columnWidth
209+
210+
Declare text width for columns by character length (or remove).
211+
212+
* Type : Array<number>|number|null
213+
* Default: null
214+
215+
**For Each Columns**
216+
217+
An array of numbers could be specified.
218+
Its each elements are set to the column at that position.
219+
A `null` as the element value means that width will not be specified.
220+
221+
**For All Columns**
222+
223+
If a single number is specified for this option, It will set to all columns.
224+
And also when the value is null, column width is not declared at all.
225+
208226
#### ListIt#setColumnWidth(indexOfColumns:number, width:number)
209227

210228
Set the column width by text length.
@@ -214,10 +232,27 @@ A number data never be affected, because it should not be truncated.
214232
So it may be longer than the specified length when some number data
215233
exist in a column.
216234

217-
**PARAMETERS**
235+
PARAMETERS:
218236

219237
1. `indexOfColumns` - a column index to set.
220238
2. `width` - a character length of the column.
239+
If `null` is specified, the declaration is removed.
240+
241+
RETURN VALUE:
242+
243+
This method returns `this` instance to chain the next method call.
244+
245+
#### ListIt#setColumnWidthAll(widthForAll:Array<number|null>|number|null)
246+
247+
Set the whole column's width. See opt.columnWidth
248+
249+
PARAMETERS:
250+
251+
1. `widthForAll` - An array of widtha.
252+
253+
RETURN VALUE:
254+
255+
This method returns `this` instance to chain the next method call.
221256

222257
### ListIt#d( data [, data ...] )
223258

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const ListItBuffer = require("./lib/list-it-buffer.js");
44
/**
55
* @typedef {object} ListItOption
66
* @property {boolean} autoAlign - Align number vertical with its decimal point.
7+
* @property {Array<number>|number|null} columnWidth - Column width by character length.
78
*/
89

910
/**

lib/list-it-buffer.js

+60-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const Column = require("./column.js");
66
/**
77
* @typedef {object} ListItOption
88
* @property {boolean} autoAlign - Align number vertical with its decimal point.
9+
* @property {Array<number>|number|null} columnWidth - Column width by character length.
910
*/
1011

1112
/**
@@ -17,6 +18,7 @@ const Column = require("./column.js");
1718
function ListItBuffer(opt) {
1819
this.opt = {
1920
"autoAlign" : true,
21+
"columnWidth": null,
2022
};
2123
if(opt) {
2224
Object.keys(this.opt).forEach(key => {
@@ -27,7 +29,7 @@ function ListItBuffer(opt) {
2729
}
2830
this.lines = [];
2931
this.columns = [];
30-
this.columnWidth = [];
32+
this.columnWidth = this.opt.columnWidth;
3133
}
3234

3335
module.exports = ListItBuffer;
@@ -162,7 +164,7 @@ ListItBuffer.prototype.pushNewRow = function() {
162164
ListItBuffer.prototype.toString = function() {
163165
const rows = [];
164166
this.columns.forEach((column, iCol)=>{
165-
column.setTextWidth(this.columnWidth[iCol]);
167+
column.setTextWidth(this.getColumnWidth(iCol));
166168
column.updateWidth();
167169
});
168170
this.lines.forEach(line => {
@@ -208,11 +210,63 @@ function objectArrayToTable(objects) {
208210
* A number data never be affected, because it should not be truncated.
209211
* So it may be longer than the specified length when some number data
210212
* exist in a column.
211-
*
213+
*
212214
* @param {number} indexOfColumns a column index to set
213215
* @param {number} width a character length of the column
214-
* @returns {undefined}
216+
* @returns {ListItBuffer} Returns this to be able to chain.
215217
*/
216218
ListItBuffer.prototype.setColumnWidth = function(indexOfColumns, width) {
217-
this.columnWidth[indexOfColumns] = width;
218-
};
219+
this.columnWidth = this.columnWidth || [];
220+
if(typeof indexOfColumns !== "number") {
221+
throw new Error("indexOfColumns must be a number");
222+
}
223+
if(!width) {
224+
delete this.columnWidth[indexOfColumns];
225+
} else {
226+
if(typeof width !== "number") {
227+
throw new Error("width must be a number");
228+
}
229+
this.columnWidth[indexOfColumns] = width;
230+
}
231+
return this;
232+
};
233+
234+
/**
235+
* Set width for all columns.
236+
*
237+
* @param {Array<number>|number|null} widthForAll a character length of the column
238+
* @returns {ListItBuffer} Returns this to be able to chain.
239+
*/
240+
ListItBuffer.prototype.setColumnWidthAll = function(widthForAll) {
241+
if(widthForAll != null
242+
&& !Array.isArray(widthForAll)
243+
&& typeof widthForAll !== "number")
244+
{
245+
throw new Error(
246+
"widthForAll must be one of a null, an Array or a number");
247+
}
248+
if(Array.isArray(widthForAll)) {
249+
for(let i = 0; i < widthForAll.length; i++) {
250+
const width = widthForAll[i];
251+
if(width != undefined && typeof width !== "number") {
252+
throw new Error(
253+
`widthForAll[${i}] must be an undefined or a number`);
254+
}
255+
}
256+
}
257+
this.columnWidth = widthForAll;
258+
return this;
259+
};
260+
261+
/**
262+
* Get the column width.
263+
*
264+
* @param {number} indexOfColumns a column index to set
265+
* @returns {number} a column width.
266+
*/
267+
ListItBuffer.prototype.getColumnWidth = function(indexOfColumns) {
268+
if(Array.isArray(this.columnWidth)) {
269+
return this.columnWidth[indexOfColumns];
270+
}
271+
return this.columnWidth;
272+
};

test/list-it.test.js

+153
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,48 @@ describe("ListIt", () => {
1414
" 12 12.3 111\n" +
1515
"123 1.23 0");
1616
});
17+
describe("opt.columnWidth", ()=>{
18+
it("should truncate the texts with the width", ()=>{
19+
const listit = new ListIt({columnWidth:[3]});
20+
listit.d([
21+
["ABCDEFG", "OPQRSTU"],
22+
["HIJKLMN", "VWXYZ"],
23+
]);
24+
assert.equal(listit.toString(),
25+
"ABC OPQRSTU\n" +
26+
"HIJ VWXYZ ");
27+
});
28+
it("should truncate the texts even if it represents number", ()=>{
29+
const listit = new ListIt({columnWidth:[,3]});
30+
listit.d([
31+
["123456", "123456"],
32+
["123456", "123456"],
33+
]);
34+
assert.equal(listit.toString(),
35+
"123456 123\n" +
36+
"123456 123");
37+
});
38+
it("should not affect when a text converted from number data which is longer than the specified length exists in column", ()=>{
39+
const listit = new ListIt({columnWidth:3});
40+
listit.d([
41+
["ABCDEFGOPQRSTU", 1.2],
42+
[123.456, "VWXYZ"],
43+
]);
44+
assert.equal(listit.toString(),
45+
"ABCDEFG 1.2\n" +
46+
"123.456 VWX");
47+
});
48+
it("should not affect when all the text is shorter than the specified length", ()=>{
49+
const listit = new ListIt({columnWidth:6});
50+
listit.d([
51+
["ABCDEFGOPQRSTU", 1.2],
52+
[123.456, "VWXYZ"],
53+
]);
54+
assert.equal(listit.toString(),
55+
"ABCDEFG 1.2\n" +
56+
"123.456 VWXYZ");
57+
});
58+
});
1759
});
1860
describe(".buffer", () => {
1961
it("should not set the autoAlign option", () => {
@@ -39,6 +81,47 @@ describe("ListIt", () => {
3981
});
4082
});
4183
describe(".setColumnWidth", ()=>{
84+
it("should return the instance", ()=>{
85+
const listit = new ListIt();
86+
assert.deepEqual(listit.setColumnWidth(0, 10), listit);
87+
});
88+
it("should throw the index is not a number", ()=>{
89+
assert.throws(()=>{
90+
const listit = new ListIt();
91+
listit.d([
92+
["ABCDEFG", "OPQRSTU"],
93+
["HIJKLMN", "VWXYZ"],
94+
]).setColumnWidth("", 10);
95+
assert.equal(listit.toString(),
96+
"ABC OPQRSTU\n" +
97+
"HIJ VWXYZ ");
98+
99+
});
100+
});
101+
it("should throw the width is not a number", ()=>{
102+
assert.throws(()=>{
103+
const listit = new ListIt();
104+
listit.d([
105+
["ABCDEFG", "OPQRSTU"],
106+
["HIJKLMN", "VWXYZ"],
107+
]).setColumnWidth(0, "");
108+
assert.equal(listit.toString(),
109+
"ABC OPQRSTU\n" +
110+
"HIJ VWXYZ ");
111+
112+
});
113+
});
114+
it("should accept null for width that remove the previous specification", ()=>{
115+
const listit = new ListIt();
116+
listit.d([
117+
["ABCDEFG", "OPQRSTU"],
118+
["HIJKLMN", "VWXYZ"],
119+
]).setColumnWidth(0, 3);
120+
listit.setColumnWidth(0, null);
121+
assert.equal(listit.toString(),
122+
"ABCDEFG OPQRSTU\n" +
123+
"HIJKLMN VWXYZ ");
124+
});
42125
it("should truncate the texts with the width", ()=>{
43126
const listit = new ListIt();
44127
listit.d([
@@ -80,4 +163,74 @@ describe("ListIt", () => {
80163
" 123.456 VWXYZ");
81164
});
82165
});
166+
describe(".setColumnWidthAll", ()=>{
167+
it("should return the instance", ()=>{
168+
const listit = new ListIt();
169+
assert.deepEqual(listit.setColumnWidthAll(0, 10), listit);
170+
});
171+
it("should throw the width is not a number", ()=>{
172+
assert.throws(()=>{
173+
const listit = new ListIt();
174+
listit.d([
175+
["ABCDEFG", "OPQRSTU"],
176+
["HIJKLMN", "VWXYZ"],
177+
]).setColumnWidthAll("");
178+
assert.equal(listit.toString(),
179+
"ABC OPQRSTU\n" +
180+
"HIJ VWXYZ ");
181+
182+
});
183+
});
184+
it("should accept null for width that remove the previous specification", ()=>{
185+
const listit = new ListIt();
186+
listit.d([
187+
["ABCDEFG", "OPQRSTU"],
188+
["HIJKLMN", "VWXYZ"],
189+
]).setColumnWidth(0, 3);
190+
listit.setColumnWidthAll(null);
191+
assert.equal(listit.toString(),
192+
"ABCDEFG OPQRSTU\n" +
193+
"HIJKLMN VWXYZ ");
194+
});
195+
it("should truncate the texts with the width", ()=>{
196+
const listit = new ListIt();
197+
listit.d([
198+
["ABCDEFG", "OPQRSTU"],
199+
["HIJKLMN", "VWXYZ"],
200+
]).setColumnWidthAll([3]);
201+
assert.equal(listit.toString(),
202+
"ABC OPQRSTU\n" +
203+
"HIJ VWXYZ ");
204+
});
205+
it("should truncate the texts even if it represents number", ()=>{
206+
const listit = new ListIt();
207+
listit.d([
208+
["123456", "123456"],
209+
["123456", "123456"],
210+
]).setColumnWidthAll([undefined, 3]);
211+
assert.equal(listit.toString(),
212+
"123456 123\n" +
213+
"123456 123");
214+
});
215+
it("should not affect when a text converted from number data which is longer than the specified length exists in column", ()=>{
216+
const listit = new ListIt();
217+
listit.d([
218+
["ABCDEFGOPQRSTU", 1.2],
219+
[123.456, "VWXYZ"],
220+
]).setColumnWidthAll(3);
221+
assert.equal(listit.toString(),
222+
"ABCDEFG 1.2\n" +
223+
"123.456 VWX");
224+
});
225+
it("should not affect when all the text is shorter than the specified length", ()=>{
226+
const listit = new ListIt();
227+
listit.d([
228+
["ABCDEFGOPQRSTU", 1.2],
229+
[123.456, "VWXYZ"],
230+
]).setColumnWidthAll(6);
231+
assert.equal(listit.toString(),
232+
"ABCDEFG 1.2\n" +
233+
"123.456 VWXYZ");
234+
});
235+
});
83236
});

0 commit comments

Comments
 (0)