Skip to content

Commit

Permalink
Merge pull request #28 from takamin/column-width-option
Browse files Browse the repository at this point in the history
Add columnWidth option and setColumnWidthAll method
  • Loading branch information
takamin authored Feb 18, 2020
2 parents 7d86711 + f4d6627 commit 81834ab
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 7 deletions.
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,24 @@ The number will be aligned to the right taking account of its decimal point.
* Type : boolean
* Default setting : true

#### opt.columnWidth

Declare text width for columns by character length (or remove).

* Type : Array<number>|number|null
* Default: null

**For Each Columns**

An array of numbers could be specified.
Its each elements are set to the column at that position.
A `null` as the element value means that width will not be specified.

**For All Columns**

If a single number is specified for this option, It will set to all columns.
And also when the value is null, column width is not declared at all.

#### ListIt#setColumnWidth(indexOfColumns:number, width:number)

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

**PARAMETERS**
PARAMETERS:

1. `indexOfColumns` - a column index to set.
2. `width` - a character length of the column.
If `null` is specified, the declaration is removed.

RETURN VALUE:

This method returns `this` instance to chain the next method call.

#### ListIt#setColumnWidthAll(widthForAll:Array<number|null>|number|null)

Set the whole column's width. See opt.columnWidth

PARAMETERS:

1. `widthForAll` - An array of widtha.

RETURN VALUE:

This method returns `this` instance to chain the next method call.

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

Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const ListItBuffer = require("./lib/list-it-buffer.js");
/**
* @typedef {object} ListItOption
* @property {boolean} autoAlign - Align number vertical with its decimal point.
* @property {Array<number>|number|null} columnWidth - Column width by character length.
*/

/**
Expand Down
66 changes: 60 additions & 6 deletions lib/list-it-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const Column = require("./column.js");
/**
* @typedef {object} ListItOption
* @property {boolean} autoAlign - Align number vertical with its decimal point.
* @property {Array<number>|number|null} columnWidth - Column width by character length.
*/

/**
Expand All @@ -17,6 +18,7 @@ const Column = require("./column.js");
function ListItBuffer(opt) {
this.opt = {
"autoAlign" : true,
"columnWidth": null,
};
if(opt) {
Object.keys(this.opt).forEach(key => {
Expand All @@ -27,7 +29,7 @@ function ListItBuffer(opt) {
}
this.lines = [];
this.columns = [];
this.columnWidth = [];
this.columnWidth = this.opt.columnWidth;
}

module.exports = ListItBuffer;
Expand Down Expand Up @@ -162,7 +164,7 @@ ListItBuffer.prototype.pushNewRow = function() {
ListItBuffer.prototype.toString = function() {
const rows = [];
this.columns.forEach((column, iCol)=>{
column.setTextWidth(this.columnWidth[iCol]);
column.setTextWidth(this.getColumnWidth(iCol));
column.updateWidth();
});
this.lines.forEach(line => {
Expand Down Expand Up @@ -208,11 +210,63 @@ function objectArrayToTable(objects) {
* A number data never be affected, because it should not be truncated.
* So it may be longer than the specified length when some number data
* exist in a column.
*
*
* @param {number} indexOfColumns a column index to set
* @param {number} width a character length of the column
* @returns {undefined}
* @returns {ListItBuffer} Returns this to be able to chain.
*/
ListItBuffer.prototype.setColumnWidth = function(indexOfColumns, width) {
this.columnWidth[indexOfColumns] = width;
};
this.columnWidth = this.columnWidth || [];
if(typeof indexOfColumns !== "number") {
throw new Error("indexOfColumns must be a number");
}
if(!width) {
delete this.columnWidth[indexOfColumns];
} else {
if(typeof width !== "number") {
throw new Error("width must be a number");
}
this.columnWidth[indexOfColumns] = width;
}
return this;
};

/**
* Set width for all columns.
*
* @param {Array<number>|number|null} widthForAll a character length of the column
* @returns {ListItBuffer} Returns this to be able to chain.
*/
ListItBuffer.prototype.setColumnWidthAll = function(widthForAll) {
if(widthForAll != null
&& !Array.isArray(widthForAll)
&& typeof widthForAll !== "number")
{
throw new Error(
"widthForAll must be one of a null, an Array or a number");
}
if(Array.isArray(widthForAll)) {
for(let i = 0; i < widthForAll.length; i++) {
const width = widthForAll[i];
if(width != undefined && typeof width !== "number") {
throw new Error(
`widthForAll[${i}] must be an undefined or a number`);
}
}
}
this.columnWidth = widthForAll;
return this;
};

/**
* Get the column width.
*
* @param {number} indexOfColumns a column index to set
* @returns {number} a column width.
*/
ListItBuffer.prototype.getColumnWidth = function(indexOfColumns) {
if(Array.isArray(this.columnWidth)) {
return this.columnWidth[indexOfColumns];
}
return this.columnWidth;
};
153 changes: 153 additions & 0 deletions test/list-it.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,48 @@ describe("ListIt", () => {
" 12 12.3 111\n" +
"123 1.23 0");
});
describe("opt.columnWidth", ()=>{
it("should truncate the texts with the width", ()=>{
const listit = new ListIt({columnWidth:[3]});
listit.d([
["ABCDEFG", "OPQRSTU"],
["HIJKLMN", "VWXYZ"],
]);
assert.equal(listit.toString(),
"ABC OPQRSTU\n" +
"HIJ VWXYZ ");
});
it("should truncate the texts even if it represents number", ()=>{
const listit = new ListIt({columnWidth:[,3]});
listit.d([
["123456", "123456"],
["123456", "123456"],
]);
assert.equal(listit.toString(),
"123456 123\n" +
"123456 123");
});
it("should not affect when a text converted from number data which is longer than the specified length exists in column", ()=>{
const listit = new ListIt({columnWidth:3});
listit.d([
["ABCDEFGOPQRSTU", 1.2],
[123.456, "VWXYZ"],
]);
assert.equal(listit.toString(),
"ABCDEFG 1.2\n" +
"123.456 VWX");
});
it("should not affect when all the text is shorter than the specified length", ()=>{
const listit = new ListIt({columnWidth:6});
listit.d([
["ABCDEFGOPQRSTU", 1.2],
[123.456, "VWXYZ"],
]);
assert.equal(listit.toString(),
"ABCDEFG 1.2\n" +
"123.456 VWXYZ");
});
});
});
describe(".buffer", () => {
it("should not set the autoAlign option", () => {
Expand All @@ -39,6 +81,47 @@ describe("ListIt", () => {
});
});
describe(".setColumnWidth", ()=>{
it("should return the instance", ()=>{
const listit = new ListIt();
assert.deepEqual(listit.setColumnWidth(0, 10), listit);
});
it("should throw the index is not a number", ()=>{
assert.throws(()=>{
const listit = new ListIt();
listit.d([
["ABCDEFG", "OPQRSTU"],
["HIJKLMN", "VWXYZ"],
]).setColumnWidth("", 10);
assert.equal(listit.toString(),
"ABC OPQRSTU\n" +
"HIJ VWXYZ ");

});
});
it("should throw the width is not a number", ()=>{
assert.throws(()=>{
const listit = new ListIt();
listit.d([
["ABCDEFG", "OPQRSTU"],
["HIJKLMN", "VWXYZ"],
]).setColumnWidth(0, "");
assert.equal(listit.toString(),
"ABC OPQRSTU\n" +
"HIJ VWXYZ ");

});
});
it("should accept null for width that remove the previous specification", ()=>{
const listit = new ListIt();
listit.d([
["ABCDEFG", "OPQRSTU"],
["HIJKLMN", "VWXYZ"],
]).setColumnWidth(0, 3);
listit.setColumnWidth(0, null);
assert.equal(listit.toString(),
"ABCDEFG OPQRSTU\n" +
"HIJKLMN VWXYZ ");
});
it("should truncate the texts with the width", ()=>{
const listit = new ListIt();
listit.d([
Expand Down Expand Up @@ -80,4 +163,74 @@ describe("ListIt", () => {
" 123.456 VWXYZ");
});
});
describe(".setColumnWidthAll", ()=>{
it("should return the instance", ()=>{
const listit = new ListIt();
assert.deepEqual(listit.setColumnWidthAll(0, 10), listit);
});
it("should throw the width is not a number", ()=>{
assert.throws(()=>{
const listit = new ListIt();
listit.d([
["ABCDEFG", "OPQRSTU"],
["HIJKLMN", "VWXYZ"],
]).setColumnWidthAll("");
assert.equal(listit.toString(),
"ABC OPQRSTU\n" +
"HIJ VWXYZ ");

});
});
it("should accept null for width that remove the previous specification", ()=>{
const listit = new ListIt();
listit.d([
["ABCDEFG", "OPQRSTU"],
["HIJKLMN", "VWXYZ"],
]).setColumnWidth(0, 3);
listit.setColumnWidthAll(null);
assert.equal(listit.toString(),
"ABCDEFG OPQRSTU\n" +
"HIJKLMN VWXYZ ");
});
it("should truncate the texts with the width", ()=>{
const listit = new ListIt();
listit.d([
["ABCDEFG", "OPQRSTU"],
["HIJKLMN", "VWXYZ"],
]).setColumnWidthAll([3]);
assert.equal(listit.toString(),
"ABC OPQRSTU\n" +
"HIJ VWXYZ ");
});
it("should truncate the texts even if it represents number", ()=>{
const listit = new ListIt();
listit.d([
["123456", "123456"],
["123456", "123456"],
]).setColumnWidthAll([undefined, 3]);
assert.equal(listit.toString(),
"123456 123\n" +
"123456 123");
});
it("should not affect when a text converted from number data which is longer than the specified length exists in column", ()=>{
const listit = new ListIt();
listit.d([
["ABCDEFGOPQRSTU", 1.2],
[123.456, "VWXYZ"],
]).setColumnWidthAll(3);
assert.equal(listit.toString(),
"ABCDEFG 1.2\n" +
"123.456 VWX");
});
it("should not affect when all the text is shorter than the specified length", ()=>{
const listit = new ListIt();
listit.d([
["ABCDEFGOPQRSTU", 1.2],
[123.456, "VWXYZ"],
]).setColumnWidthAll(6);
assert.equal(listit.toString(),
"ABCDEFG 1.2\n" +
"123.456 VWXYZ");
});
});
});

0 comments on commit 81834ab

Please sign in to comment.