Skip to content

Commit 1d7795c

Browse files
committed
Set row and column attributes
1 parent 4f8650f commit 1d7795c

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

index.js

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@ const codesANSI = {
4949
},
5050
};
5151

52-
function getCellText(content, colWidth, cellAttrs) {
52+
function getCellText(content, colWidth, cellAttrs, rowAttrs, colAttrs) {
5353
var attrs;
5454
if (typeof cellAttrs === "object") attrs = cellAttrs;
55+
else if (typeof rowAttrs === "object") attrs = rowAttrs;
56+
else if (typeof colAttrs === "object") attrs = colAttrs;
5557
else attrs = {};
5658

5759
var columnStr = "";
@@ -264,7 +266,7 @@ class Table {
264266
if (table.length > 0) {
265267
stringTable += "\n";
266268
for (var i = 0; i < table[0].length; i++) {
267-
stringTable += this.borders.sep + " ".repeat(this.leftPadding) + getCellText(table[0][i], columnsLengths[i], this.attrs[`cell:${0},${i}`]) + " ".repeat(this.rightPadding);
269+
stringTable += this.borders.sep + " ".repeat(this.leftPadding) + getCellText(table[0][i], columnsLengths[i], this.attrs[`cell:${0},${i}`], this.attrs[`row:${0}`], this.attrs[`col:${i}`]) + " ".repeat(this.rightPadding);
268270
}
269271
stringTable += this.borders.sep;
270272
}
@@ -281,7 +283,7 @@ class Table {
281283
if (table[i] !== "HORIZONTAL LINE") {
282284
stringTable += "\n";
283285
for (var j = 0; j < table[i].length; j++) {
284-
stringTable += this.borders.sep + " ".repeat(this.leftPadding) + getCellText(table[i][j], columnsLengths[j], this.attrs[`cell:${i},${j}`]) + " ".repeat(this.rightPadding);
286+
stringTable += this.borders.sep + " ".repeat(this.leftPadding) + getCellText(table[i][j], columnsLengths[j], this.attrs[`cell:${i},${j}`], this.attrs[`row:${i}`], this.attrs[`col:${j}`]) + " ".repeat(this.rightPadding);
285287
}
286288
stringTable += this.borders.sep;
287289
if (this.horizontalLines && i < table.length - 1) {
@@ -349,6 +351,38 @@ class Table {
349351
this.attrs[`cell:${row},${col}`] = attrs;
350352
} else throw TypeError("attrs must be an object or undefined");
351353
}
354+
355+
setRowAttrs(row, attrs) {
356+
if (row >= this.rows.length) throw RangeError("row number out of range");
357+
if (this.rows[row] === "HORIZONTAL LINE") throw SyntaxError("this row is horizontal line");
358+
if (typeof attrs === "undefined") delete this.attrs[`row:${row}`];
359+
else if (typeof attrs === "object") {
360+
Object.keys(attrs).forEach((key) => { if (!["color", "bgColor", "decorations", "align"].includes(key)) throw SyntaxError(`invalid attribute "${key}"`); });
361+
if (typeof attrs.color !== "string" && typeof attrs.color !== "undefined") throw TypeError("attrs.color must be a string or undefined");
362+
if (typeof attrs.bgColor !== "string" && typeof attrs.bgColor !== "undefined") throw TypeError("attrs.bgColor must be a string or undefined");
363+
if (!Array.isArray(attrs.decorations) && typeof attrs.decorations !== "undefined") throw TypeError("attrs.decorations must be an array or undefined");
364+
if (typeof attrs.color === "string" && !codesANSI.foreground.hasOwnProperty(attrs.color)) throw SyntaxError("attrs.color is invalid");
365+
if (typeof attrs.bgColor === "string" && !codesANSI.background.hasOwnProperty(attrs.bgColor)) throw SyntaxError("attrs.bgColor is invalid");
366+
if (Array.isArray(attrs.decorations)) attrs.decorations.forEach((name) => { if (!Object.keys(codesANSI.decorations).includes(name)) throw SyntaxError(`invalid decoration "${name}"`); });
367+
if (typeof attrs.align !== "undefined" && attrs.align !== "left" && attrs.align !== "center" && attrs.align !== "right") throw SyntaxError('attrs.align must be "left", "center", "right" or undefined');
368+
this.attrs[`row:${row}`] = attrs;
369+
} else throw TypeError("attrs must be an object or undefined");
370+
}
371+
372+
setColAttrs(col, attrs) {
373+
if (typeof attrs === "undefined") delete this.attrs[`col:${col}`];
374+
else if (typeof attrs === "object") {
375+
Object.keys(attrs).forEach((key) => { if (!["color", "bgColor", "decorations", "align"].includes(key)) throw SyntaxError(`invalid attribute "${key}"`); });
376+
if (typeof attrs.color !== "string" && typeof attrs.color !== "undefined") throw TypeError("attrs.color must be a string or undefined");
377+
if (typeof attrs.bgColor !== "string" && typeof attrs.bgColor !== "undefined") throw TypeError("attrs.bgColor must be a string or undefined");
378+
if (!Array.isArray(attrs.decorations) && typeof attrs.decorations !== "undefined") throw TypeError("attrs.decorations must be an array or undefined");
379+
if (typeof attrs.color === "string" && !codesANSI.foreground.hasOwnProperty(attrs.color)) throw SyntaxError("attrs.color is invalid");
380+
if (typeof attrs.bgColor === "string" && !codesANSI.background.hasOwnProperty(attrs.bgColor)) throw SyntaxError("attrs.bgColor is invalid");
381+
if (Array.isArray(attrs.decorations)) attrs.decorations.forEach((name) => { if (!Object.keys(codesANSI.decorations).includes(name)) throw SyntaxError(`invalid decoration "${name}"`); });
382+
if (typeof attrs.align !== "undefined" && attrs.align !== "left" && attrs.align !== "center" && attrs.align !== "right") throw SyntaxError('attrs.align must be "left", "center", "right" or undefined');
383+
this.attrs[`col:${col}`] = attrs;
384+
} else throw TypeError("attrs must be an object or undefined");
385+
}
352386
}
353387

354388
module.exports = Table;

0 commit comments

Comments
 (0)