diff --git a/.README/README.md b/.README/README.md
index 82b6949..993adf0 100644
--- a/.README/README.md
+++ b/.README/README.md
@@ -27,6 +27,7 @@ Produces a string that represents array data in a text table.
{"gitdown": "include", "file": "./usage/cell_content_alignment.md"}
{"gitdown": "include", "file": "./usage/column_width.md"}
{"gitdown": "include", "file": "./usage/custom_border.md"}
+{"gitdown": "include", "file": "./usage/draw_vertical_line.md"}
{"gitdown": "include", "file": "./usage/draw_horizontal_line.md"}
{"gitdown": "include", "file": "./usage/single_line_mode.md"}
{"gitdown": "include", "file": "./usage/padding_cell_content.md"}
diff --git a/README.md b/README.md
index 22a7996..939084d 100644
--- a/README.md
+++ b/README.md
@@ -14,6 +14,7 @@
* [Cell Content Alignment](#table-usage-cell-content-alignment)
* [Column Width](#table-usage-column-width)
* [Custom Border](#table-usage-custom-border)
+ * [Draw Vertical Line](#table-usage-draw-vertical-line)
* [Draw Horizontal Line](#table-usage-draw-horizontal-line)
* [Single Line Mode](#table-usage-single-line-mode)
* [Padding Cell Content](#table-usage-padding-cell-content)
@@ -292,6 +293,57 @@ console.log(output);
└────┴────┴────┘
```
+
+### Draw Vertical Line
+
+`{function} config.drawVerticalLine` property is a function that is called for every non-content column in the table. The result of the function `{boolean}` determines whether a border is drawn.
+
+```js
+let data,
+ output,
+ options;
+
+data = [
+ ['0A', '0B', '0C'],
+ ['1A', '1B', '1C'],
+ ['2A', '2B', '2C'],
+ ['3A', '3B', '3C'],
+ ['4A', '4B', '4C']
+];
+
+options = {
+ /**
+ * @typedef {function} drawVerticalLine
+ * @param {number} index
+ * @param {number} size
+ * @return {boolean}
+ */
+ drawVerticalLine: (index, size) => {
+ return index === 0 || index === size;
+ }
+};
+
+output = table(data, options);
+
+console.log(output);
+
+```
+
+```
+╔════════════╗
+║ 0A 0B 0C ║
+╟────────────╢
+║ 1A 1B 1C ║
+╟────────────╢
+║ 2A 2B 2C ║
+╟────────────╢
+║ 3A 3B 3C ║
+╟────────────╢
+║ 4A 4B 4C ║
+╚════════════╝
+
+```
+
### Draw Horizontal Line
diff --git a/src/createStream.js b/src/createStream.js
index 8c9c59f..2c2f164 100644
--- a/src/createStream.js
+++ b/src/createStream.js
@@ -14,7 +14,7 @@ import truncateTableData from './truncateTableData';
/**
* @param {Array} data
- * @param {object} config
+ * @param {streamConfig} config
* @returns {Array}
*/
const prepareData = (data, config) => {
@@ -36,23 +36,23 @@ const prepareData = (data, config) => {
/**
* @param {string[]} row
* @param {number[]} columnWidthIndex
- * @param {object} config
+ * @param {streamConfig} config
* @returns {undefined}
*/
const create = (row, columnWidthIndex, config) => {
const rows = prepareData([row], config);
const body = rows.map((literalRow) => {
- return drawRow(literalRow, config.border);
+ return drawRow(literalRow, config);
}).join('');
let output;
output = '';
- output += drawBorderTop(columnWidthIndex, config.border);
+ output += drawBorderTop(columnWidthIndex, config);
output += body;
- output += drawBorderBottom(columnWidthIndex, config.border);
+ output += drawBorderBottom(columnWidthIndex, config);
output = output.trimEnd();
@@ -62,24 +62,24 @@ const create = (row, columnWidthIndex, config) => {
/**
* @param {string[]} row
* @param {number[]} columnWidthIndex
- * @param {object} config
+ * @param {streamConfig} config
* @returns {undefined}
*/
const append = (row, columnWidthIndex, config) => {
const rows = prepareData([row], config);
const body = rows.map((literalRow) => {
- return drawRow(literalRow, config.border);
+ return drawRow(literalRow, config);
}).join('');
let output = '';
- const bottom = drawBorderBottom(columnWidthIndex, config.border);
+ const bottom = drawBorderBottom(columnWidthIndex, config);
if (bottom !== '\n') {
output = '\r\u001B[K';
}
- output += drawBorderJoin(columnWidthIndex, config.border);
+ output += drawBorderJoin(columnWidthIndex, config);
output += body;
output += bottom;
diff --git a/src/drawBorder.js b/src/drawBorder.js
index 85de247..fc94622 100644
--- a/src/drawBorder.js
+++ b/src/drawBorder.js
@@ -1,5 +1,7 @@
+import drawHorizontalContent from './drawHorizontalContent';
+
/**
- * @typedef drawBorder~parts
+ * @typedef drawBorder~border
* @property {string} left
* @property {string} right
* @property {string} body
@@ -8,88 +10,74 @@
/**
* @param {number[]} columnSizeIndex
- * @param {drawBorder~parts} parts
+ * @param {object} config
+ * @param {drawBorder~border} config.border
+ * @param {Function} config.drawVerticalLine
* @returns {string}
*/
-const drawBorder = (columnSizeIndex, parts) => {
- const columns = columnSizeIndex
- .map((size) => {
- return parts.body.repeat(size);
- })
- .join(parts.join);
+const drawBorder = (columnSizeIndex, {border, drawVerticalLine}) => {
+ const columns = columnSizeIndex.map((size) => {
+ return border.body.repeat(size);
+ });
- return parts.left + columns + parts.right + '\n';
+ return drawHorizontalContent(columns, {
+ drawVerticalLine,
+ separator: border,
+ });
};
-/**
- * @typedef drawBorderTop~parts
- * @property {string} topLeft
- * @property {string} topRight
- * @property {string} topBody
- * @property {string} topJoin
- */
-
/**
* @param {number[]} columnSizeIndex
- * @param {drawBorderTop~parts} parts
+ * @param {table~config} config
* @returns {string}
*/
-const drawBorderTop = (columnSizeIndex, parts) => {
- const border = drawBorder(columnSizeIndex, {
- body: parts.topBody,
- join: parts.topJoin,
- left: parts.topLeft,
- right: parts.topRight,
- });
+const drawBorderTop = (columnSizeIndex, {border, drawVerticalLine}) => {
+ const result = drawBorder(columnSizeIndex, {border: {
+ body: border.topBody,
+ join: border.topJoin,
+ left: border.topLeft,
+ right: border.topRight,
+ },
+ drawVerticalLine});
- if (border === '\n') {
+ if (result === '\n') {
return '';
}
- return border;
+ return result;
};
-/**
- * @typedef drawBorderJoin~parts
- * @property {string} joinLeft
- * @property {string} joinRight
- * @property {string} joinBody
- * @property {string} joinJoin
- */
-
/**
* @param {number[]} columnSizeIndex
- * @param {drawBorderJoin~parts} parts
+ * @param {table~config} config
* @returns {string}
*/
-const drawBorderJoin = (columnSizeIndex, parts) => {
+const drawBorderJoin = (columnSizeIndex, {border, drawVerticalLine}) => {
return drawBorder(columnSizeIndex, {
- body: parts.joinBody,
- join: parts.joinJoin,
- left: parts.joinLeft,
- right: parts.joinRight,
+ border: {
+ body: border.joinBody,
+ join: border.joinJoin,
+ left: border.joinLeft,
+ right: border.joinRight,
+ },
+ drawVerticalLine,
});
};
-/**
- * @typedef drawBorderBottom~parts
- * @property {string} topLeft
- * @property {string} topRight
- * @property {string} topBody
- * @property {string} topJoin
- */
-
/**
* @param {number[]} columnSizeIndex
- * @param {drawBorderBottom~parts} parts
+ * @param {table~config} config
* @returns {string}
*/
-const drawBorderBottom = (columnSizeIndex, parts) => {
+const drawBorderBottom = (columnSizeIndex, {border, drawVerticalLine}) => {
return drawBorder(columnSizeIndex, {
- body: parts.bottomBody,
- join: parts.bottomJoin,
- left: parts.bottomLeft,
- right: parts.bottomRight,
+ border: {
+ body: border.bottomBody,
+ join: border.bottomJoin,
+ left: border.bottomLeft,
+ right: border.bottomRight,
+ },
+ drawVerticalLine,
});
};
diff --git a/src/drawHorizontalContent.js b/src/drawHorizontalContent.js
new file mode 100644
index 0000000..5b7ac49
--- /dev/null
+++ b/src/drawHorizontalContent.js
@@ -0,0 +1,31 @@
+/**
+ * @typedef {object} drawHorizontalContent~separator
+ * @property {string} left
+ * @property {string} right
+ * @property {string} join
+ */
+
+/**
+ * @param {string[]} columns
+ * @param {object} config
+ * @param {drawHorizontalContent~separator} config.separator
+ * @param {Function} config.drawVerticalLine
+ * @returns {string}
+ */
+export default (columns, {separator, drawVerticalLine}) => {
+ const columnCount = columns.length;
+ const result = [];
+
+ result.push(drawVerticalLine(0, columnCount) ? separator.left : '');
+
+ columns.forEach((column, index) => {
+ result.push(column);
+
+ if (index + 1 < columnCount) {
+ result.push(drawVerticalLine(index + 1, columnCount) ? separator.join : '');
+ }
+ });
+ result.push(drawVerticalLine(columnCount, columnCount) ? separator.right : '');
+
+ return result.join('') + '\n';
+};
diff --git a/src/drawRow.js b/src/drawRow.js
index dffffbc..d185e49 100644
--- a/src/drawRow.js
+++ b/src/drawRow.js
@@ -1,3 +1,5 @@
+import drawHorizontalContent from './drawHorizontalContent';
+
/**
* @typedef {object} drawRow~border
* @property {string} bodyLeft
@@ -7,9 +9,21 @@
/**
* @param {string[]} columns
- * @param {drawRow~border} border
+ * @param {object} config
+ * @param {border} config.border
+ * @param {Function} config.drawVerticalLine
* @returns {string}
*/
-export default (columns, border) => {
- return border.bodyLeft + columns.join(border.bodyJoin) + border.bodyRight + '\n';
+export default (columns, {
+ border,
+ drawVerticalLine,
+}) => {
+ return drawHorizontalContent(columns, {
+ drawVerticalLine,
+ separator: {
+ join: border.bodyJoin,
+ left: border.bodyLeft,
+ right: border.bodyRight,
+ },
+ });
};
diff --git a/src/drawTable.js b/src/drawTable.js
index 84707ca..4026497 100644
--- a/src/drawTable.js
+++ b/src/drawTable.js
@@ -1,20 +1,26 @@
import {
- drawBorderTop,
- drawBorderJoin,
- drawBorderBottom,
+ drawBorderTop, drawBorderJoin, drawBorderBottom,
} from './drawBorder';
import drawRow from './drawRow';
/**
- * @param {Array} rows
- * @param {object} border
+ * @param {string[][]} rows
* @param {Array} columnSizeIndex
* @param {Array} rowSpanIndex
- * @param {Function} drawHorizontalLine
- * @param {boolean} singleLine
+ * @param {table~config} config
* @returns {string}
*/
-export default (rows, border, columnSizeIndex, rowSpanIndex, drawHorizontalLine, singleLine) => {
+export default (
+ rows,
+ columnSizeIndex,
+ rowSpanIndex,
+ config,
+) => {
+ const {
+ drawHorizontalLine,
+ singleLine,
+ } = config;
+
let output;
let realRowIndex;
let rowHeight;
@@ -26,11 +32,11 @@ export default (rows, border, columnSizeIndex, rowSpanIndex, drawHorizontalLine,
output = '';
if (drawHorizontalLine(realRowIndex, rowCount)) {
- output += drawBorderTop(columnSizeIndex, border);
+ output += drawBorderTop(columnSizeIndex, config);
}
rows.forEach((row, index0) => {
- output += drawRow(row, border);
+ output += drawRow(row, config);
if (!rowHeight) {
rowHeight = rowSpanIndex[realRowIndex];
@@ -40,13 +46,18 @@ export default (rows, border, columnSizeIndex, rowSpanIndex, drawHorizontalLine,
rowHeight--;
- if (!singleLine && rowHeight === 0 && index0 !== rowCount - 1 && drawHorizontalLine(realRowIndex, rowCount)) {
- output += drawBorderJoin(columnSizeIndex, border);
+ if (
+ !singleLine &&
+ rowHeight === 0 &&
+ index0 !== rowCount - 1 &&
+ drawHorizontalLine(realRowIndex, rowCount)
+ ) {
+ output += drawBorderJoin(columnSizeIndex, config);
}
});
if (drawHorizontalLine(realRowIndex, rowCount)) {
- output += drawBorderBottom(columnSizeIndex, border);
+ output += drawBorderBottom(columnSizeIndex, config);
}
return output;
diff --git a/src/makeConfig.js b/src/makeConfig.js
index 3fcbc79..ae03633 100644
--- a/src/makeConfig.js
+++ b/src/makeConfig.js
@@ -61,13 +61,22 @@ export default (rows, userConfig = {}) => {
if (!config.drawHorizontalLine) {
/**
- * @returns {boolean}
- */
+ * @returns {boolean}
+ */
config.drawHorizontalLine = () => {
return true;
};
}
+ if (!config.drawVerticalLine) {
+ /**
+ * @returns {boolean}
+ */
+ config.drawVerticalLine = () => {
+ return true;
+ };
+ }
+
if (config.singleLine === undefined) {
config.singleLine = false;
}
diff --git a/src/makeStreamConfig.js b/src/makeStreamConfig.js
index 8536bf5..1731a3e 100644
--- a/src/makeStreamConfig.js
+++ b/src/makeStreamConfig.js
@@ -79,5 +79,14 @@ export default (userConfig = {}) => {
config.border = makeBorder(config.border);
config.columns = makeColumns(config.columnCount, config.columns, config.columnDefault);
+ if (!config.drawVerticalLine) {
+ /**
+ * @returns {boolean}
+ */
+ config.drawVerticalLine = () => {
+ return true;
+ };
+ }
+
return config;
};
diff --git a/src/schemas/config.json b/src/schemas/config.json
index 10fc74a..d82828e 100644
--- a/src/schemas/config.json
+++ b/src/schemas/config.json
@@ -12,6 +12,9 @@
"columnDefault": {
"$ref": "shared.json#/definitions/column"
},
+ "drawVerticalLine": {
+ "typeof": "function"
+ },
"drawHorizontalLine": {
"typeof": "function"
},
diff --git a/src/schemas/streamConfig.json b/src/schemas/streamConfig.json
index 24dfa56..8670151 100644
--- a/src/schemas/streamConfig.json
+++ b/src/schemas/streamConfig.json
@@ -14,6 +14,9 @@
},
"columnCount": {
"type": "number"
+ },
+ "drawVerticalLine": {
+ "typeof": "function"
}
},
"additionalProperties": false
diff --git a/src/table.js b/src/table.js
index 6790e8c..887025b 100644
--- a/src/table.js
+++ b/src/table.js
@@ -46,6 +46,17 @@ import validateTableData from './validateTableData';
* @property {string} joinJoin
*/
+/**
+ * Used to tell whether to draw a vertical line.
+ * This callback is called for each non-content line of the table.
+ * The default behavior is to always return true.
+ *
+ * @typedef {Function} drawVerticalLine
+ * @param {number} index
+ * @param {number} size
+ * @returns {boolean}
+ */
+
/**
* Used to tell whether to draw a horizontal line.
* This callback is called for each non-content line of the table.
@@ -62,6 +73,7 @@ import validateTableData from './validateTableData';
* @property {table~border} border
* @property {table~columns[]} columns Column specific configuration.
* @property {table~columns} columnDefault Default values for all columns. Column specific settings overwrite the default values.
+ * @property {table~drawVerticalLine} drawVerticalLine
* @property {table~drawHorizontalLine} drawHorizontalLine
* @property {table~singleLine} singleLine Horizontal lines inside the table are not drawn.
*/
@@ -92,5 +104,5 @@ export default (data, userConfig = {}) => {
const cellWidthIndex = calculateCellWidthIndex(rows[0]);
- return drawTable(rows, config.border, cellWidthIndex, rowHeightIndex, config.drawHorizontalLine, config.singleLine);
+ return drawTable(rows, cellWidthIndex, rowHeightIndex, config);
};
diff --git a/test/README/usage/draw_vertical_line.js b/test/README/usage/draw_vertical_line.js
new file mode 100644
index 0000000..0c21710
--- /dev/null
+++ b/test/README/usage/draw_vertical_line.js
@@ -0,0 +1,44 @@
+import {
+ table,
+} from '../../../src';
+import expectTable from './expectTable';
+
+describe('README.md usage/', () => {
+ it('usage/draw_vertical_line', () => {
+ const data = [
+ ['0A', '0B', '0C'],
+ ['1A', '1B', '1C'],
+ ['2A', '2B', '2C'],
+ ['3A', '3B', '3C'],
+ ['4A', '4B', '4C'],
+ ];
+
+ const options = {
+ /**
+ * @typedef {Function} drawVerticalLine
+ * @param {number} index
+ * @param {number} size
+ * @returns {boolean}
+ */
+ drawVerticalLine: (index, size) => {
+ return index === 0 || index === size;
+ },
+ };
+
+ const output = table(data, options);
+
+ expectTable(output, `
+╔════════════╗
+║ 0A 0B 0C ║
+╟────────────╢
+║ 1A 1B 1C ║
+╟────────────╢
+║ 2A 2B 2C ║
+╟────────────╢
+║ 3A 3B 3C ║
+╟────────────╢
+║ 4A 4B 4C ║
+╚════════════╝
+ `);
+ });
+});
diff --git a/test/createStream.js b/test/createStream.js
index 8c8aa37..b611984 100644
--- a/test/createStream.js
+++ b/test/createStream.js
@@ -45,11 +45,12 @@ describe('createStream', () => {
context('write two rows', () => {
let stub;
- before(() => {
+ beforeEach(() => {
stub = SinonStub(process.stdout, 'write');
});
- after(() => {
+ afterEach(() => {
stub.restore();
+ stub.resetHistory();
});
it('process.stdout.write calls twice with proper arguments', () => {
@@ -81,5 +82,24 @@ describe('createStream', () => {
assert.calledWithExactly(stub.getCall(0), '+------+-----+-------+\n| a | cc | d |\n| b | c | |\n+------+-----+-------+');
assert.calledWithExactly(stub.getCall(1), '\r\u001b[K|------|-----|-------|\n| e | f | g |\n+------+-----+-------+');
});
+
+ context('given custom drawVerticalLine', () => {
+ it('use the callback to draw vertical lines', () => {
+ const stream = createStream({
+ columnCount: 2,
+ columnDefault: {
+ width: 2,
+ },
+ drawVerticalLine: (index) => {
+ return index === 1;
+ },
+ });
+
+ stream.write(['a', 'b']);
+ assert.callCount(stub, 1);
+
+ assert.calledOnceWithExactly(stub, '════╤════\n a │ b \n════╧════');
+ });
+ });
});
});
diff --git a/test/drawBorder.js b/test/drawBorder.js
index f8d2c94..af1d0e8 100644
--- a/test/drawBorder.js
+++ b/test/drawBorder.js
@@ -10,75 +10,128 @@ import {
drawBorderBottom,
} from '../src/drawBorder';
+const defaultDrawVerticalLine = () => {
+ return true;
+};
+
+const customDrawVerticalLine = (index, size) => {
+ return index === size - 1;
+};
+
describe('drawBorder', () => {
it('draws a border using parts', () => {
- const parts = {
- left: '╔',
- right: '╗',
- body: '═',
- join: '╤',
+ const config = {
+ border: {
+ left: '╔',
+ right: '╗',
+ body: '═',
+ join: '╤',
+ },
+ drawVerticalLine: defaultDrawVerticalLine,
};
- expect(drawBorder([1], parts)).to.equal('╔═╗\n');
- expect(drawBorder([1, 1], parts)).to.equal('╔═╤═╗\n');
- expect(drawBorder([5, 10], parts)).to.equal('╔═════╤══════════╗\n');
+ expect(drawBorder([1], config)).to.equal('╔═╗\n');
+ expect(drawBorder([1, 1], config)).to.equal('╔═╤═╗\n');
+ expect(drawBorder([5, 10], config)).to.equal('╔═════╤══════════╗\n');
+
+ expect(drawBorder([5, 10],
+ {
+ ...config,
+ drawVerticalLine: customDrawVerticalLine,
+ })).to.equal('═════╤══════════\n');
});
});
describe('drawBorderTop', () => {
it('draws a border using parts', () => {
- const parts = {
- topLeft: '╔',
- topRight: '╗',
- topBody: '═',
- topJoin: '╤',
+ const config = {
+ border: {
+ topLeft: '╔',
+ topRight: '╗',
+ topBody: '═',
+ topJoin: '╤',
+ },
+ drawVerticalLine: defaultDrawVerticalLine,
};
- expect(drawBorderTop([1], parts)).to.equal('╔═╗\n');
- expect(drawBorderTop([1, 1], parts)).to.equal('╔═╤═╗\n');
- expect(drawBorderTop([5, 10], parts)).to.equal('╔═════╤══════════╗\n');
+ expect(drawBorderTop([1], config)).to.equal('╔═╗\n');
+ expect(drawBorderTop([1, 1], config)).to.equal('╔═╤═╗\n');
+ expect(drawBorderTop([5, 10], config)).to.equal('╔═════╤══════════╗\n');
+
+ expect(drawBorderTop([5, 10],
+ {
+ ...config,
+ drawVerticalLine: customDrawVerticalLine,
+ })).to.equal('═════╤══════════\n');
});
it('no leading new line if borderless', () => {
- const parts = {
- topLeft: '',
- topRight: '',
- topBody: '',
- topJoin: '',
+ const config = {
+ border: {
+ topLeft: '',
+ topRight: '',
+ topBody: '',
+ topJoin: '',
+ },
+ drawVerticalLine: defaultDrawVerticalLine,
};
- expect(drawBorderTop([1], parts)).to.equal('');
- expect(drawBorderTop([1, 1], parts)).to.equal('');
- expect(drawBorderTop([5, 10], parts)).to.equal('');
+ expect(drawBorderTop([1], config)).to.equal('');
+ expect(drawBorderTop([1, 1], config)).to.equal('');
+ expect(drawBorderTop([5, 10], config)).to.equal('');
+
+ expect(drawBorderTop([5, 10],
+ {
+ ...config,
+ drawVerticalLine: customDrawVerticalLine,
+ })).to.equal('');
});
});
describe('drawBorderJoin', () => {
it('draws a border using parts', () => {
- const parts = {
- joinBody: '─',
- joinLeft: '╟',
- joinRight: '╢',
- joinJoin: '┼',
+ const config = {
+ border: {
+ joinBody: '─',
+ joinLeft: '╟',
+ joinRight: '╢',
+ joinJoin: '┼',
+ },
+ drawVerticalLine: defaultDrawVerticalLine,
};
- expect(drawBorderJoin([1], parts)).to.equal('╟─╢\n');
- expect(drawBorderJoin([1, 1], parts)).to.equal('╟─┼─╢\n');
- expect(drawBorderJoin([5, 10], parts)).to.equal('╟─────┼──────────╢\n');
+ expect(drawBorderJoin([1], config)).to.equal('╟─╢\n');
+ expect(drawBorderJoin([1, 1], config)).to.equal('╟─┼─╢\n');
+ expect(drawBorderJoin([5, 10], config)).to.equal('╟─────┼──────────╢\n');
+
+ expect(drawBorderJoin([5, 10],
+ {
+ ...config,
+ drawVerticalLine: customDrawVerticalLine,
+ })).to.equal('─────┼──────────\n');
});
});
describe('drawBorderBottom', () => {
it('draws a border using parts', () => {
- const parts = {
- bottomBody: '═',
- bottomJoin: '╧',
- bottomLeft: '╚',
- bottomRight: '╝',
+ const config = {
+ border: {
+ bottomBody: '═',
+ bottomJoin: '╧',
+ bottomLeft: '╚',
+ bottomRight: '╝',
+ },
+ drawVerticalLine: defaultDrawVerticalLine,
};
- expect(drawBorderBottom([1], parts)).to.equal('╚═╝\n');
- expect(drawBorderBottom([1, 1], parts)).to.equal('╚═╧═╝\n');
- expect(drawBorderBottom([5, 10], parts)).to.equal('╚═════╧══════════╝\n');
+ expect(drawBorderBottom([1], config)).to.equal('╚═╝\n');
+ expect(drawBorderBottom([1, 1], config)).to.equal('╚═╧═╝\n');
+ expect(drawBorderBottom([5, 10], config)).to.equal('╚═════╧══════════╝\n');
+
+ expect(drawBorderBottom([5, 10],
+ {
+ ...config,
+ drawVerticalLine: customDrawVerticalLine,
+ })).to.equal('═════╧══════════\n');
});
});
diff --git a/test/drawRow.js b/test/drawRow.js
index 197e485..b439b75 100644
--- a/test/drawRow.js
+++ b/test/drawRow.js
@@ -3,16 +3,59 @@ import {
} from 'chai';
import drawRow from '../src/drawRow';
+const drawVerticalLine = () => {
+ return true;
+};
+
describe('drawRow', () => {
- it('draws a row using parts', () => {
- const parts = {
- bodyJoin: '│',
- bodyLeft: '║',
- bodyRight: '║',
- };
-
- expect(drawRow([], parts)).to.equal('║║\n');
- expect(drawRow(['a'], parts)).to.equal('║a║\n');
- expect(drawRow(['a', ' b '], parts)).to.equal('║a│ b ║\n');
+ context('default drawVerticalLine', () => {
+ it('draws a row using all parts', () => {
+ const border = {
+ bodyJoin: '│',
+ bodyLeft: '║',
+ bodyRight: '║',
+ };
+ const config = {
+ border,
+ drawVerticalLine,
+ };
+
+ expect(drawRow([], config)).to.equal('║║\n');
+ expect(drawRow(['a'], config)).to.equal('║a║\n');
+ expect(drawRow(['a', ' b '], config)).to.equal('║a│ b ║\n');
+ });
+ });
+
+ context('custom drawVerticalLine', () => {
+ it('draws the vertical line when the drawVerticalLine returns true', () => {
+ const rows = [' a ', ' b ', ' c '];
+
+ const border = {
+ bodyJoin: '│',
+ bodyLeft: '║',
+ bodyRight: '║',
+ };
+
+ expect(drawRow(rows, {
+ border,
+ drawVerticalLine: (index) => {
+ return index === 0;
+ },
+ })).to.equal('║ a b c \n');
+
+ expect(drawRow(rows, {
+ border,
+ drawVerticalLine: (index) => {
+ return index % 2 === 0;
+ },
+ })).to.equal('║ a b │ c \n');
+
+ expect(drawRow(rows, {
+ border,
+ drawVerticalLine: (index, size) => {
+ return index > 0 && index <= size;
+ },
+ })).to.equal(' a │ b │ c ║\n');
+ });
});
});
diff --git a/test/makeConfig.js b/test/makeConfig.js
index b76461f..cbb7f60 100644
--- a/test/makeConfig.js
+++ b/test/makeConfig.js
@@ -6,14 +6,11 @@ import {
import makeConfig from '../src/makeConfig';
describe('makeConfig', () => {
+ const rows = [['aaaaa']];
+
it('does not affect the parameter configuration object', () => {
const config = {};
-
- makeConfig([
- [
- 'aaaaa',
- ],
- ], config);
+ makeConfig(rows, config);
expect(config).to.deep.equal({});
});
@@ -22,73 +19,133 @@ describe('makeConfig', () => {
context('"alignment"', () => {
context('is not provided', () => {
it('defaults to "left"', () => {
- const config = makeConfig([
- [
- 'aaaaa',
- ],
- ]);
+ const config = makeConfig(rows);
expect(config.columns[0].alignment).to.equal('left');
});
});
+
+ context('is provided', () => {
+ it('uses the custom value', () => {
+ const config = makeConfig(rows, {columns: {
+ 0: {alignment: 'center'},
+ }});
+
+ expect(config.columns[0].alignment).to.equal('center');
+ });
+ });
});
+
context('"width"', () => {
context('is not provided', () => {
it('defaults to the maximum column width', () => {
- const config = makeConfig([
- [
- 'aaaaa',
- ],
- ]);
+ const config = makeConfig(rows);
expect(config.columns[0].width).to.equal(5);
});
});
+
+ context('is provided', () => {
+ it('uses the custom value', () => {
+ const config = makeConfig(rows, {columns: {0: {width: 7}}});
+
+ expect(config.columns[0].width).to.equal(7);
+ });
+ });
});
+
context('"paddingLeft"', () => {
context('is not provided', () => {
it('defaults to 1', () => {
- const config = makeConfig([
- [
- 'aaaaa',
- ],
- ]);
+ const config = makeConfig(rows);
expect(config.columns[0].paddingLeft).to.equal(1);
});
});
+
+ context('is provided', () => {
+ it('uses the custom value', () => {
+ const config = makeConfig(rows, {columns: {0: {paddingLeft: 3}}});
+
+ expect(config.columns[0].paddingLeft).to.equal(3);
+ });
+ });
});
+
context('"paddingRight"', () => {
context('is not provided', () => {
it('defaults to 1', () => {
- const config = makeConfig([
- [
- 'aaaaa',
- ],
- ]);
+ const config = makeConfig(rows);
expect(config.columns[0].paddingRight).to.equal(1);
});
});
+
+ context('is provided', () => {
+ it('uses the custom value', () => {
+ const config = makeConfig(rows, {columns: {0: {paddingRight: 3}}});
+
+ expect(config.columns[0].paddingRight).to.equal(3);
+ });
+ });
});
});
- context('"drawHorizontalLine', () => {
+ context('"drawVerticalLine', () => {
context('is not provided', () => {
it('defaults to retuning true', () => {
- const config = makeConfig([['aaaaa']]);
+ const config = makeConfig(rows);
- expect(config.drawHorizontalLine()).to.equal(true);
+ expect(config.drawVerticalLine()).to.equal(true);
+ });
+ });
+
+ context('is provided', () => {
+ it('uses the custom function', () => {
+ const config = makeConfig(rows, {drawVerticalLine: () => {
+ return false;
+ }});
+
+ expect(config.drawVerticalLine()).to.equal(false);
});
});
});
+
context('"drawHorizontalLine', () => {
context('is not provided', () => {
it('defaults to retuning true', () => {
const config = makeConfig([['aaaaa']]);
+ expect(config.drawHorizontalLine()).to.equal(true);
+ });
+ });
+
+ context('is provided', () => {
+ it('uses the custom function', () => {
+ const config = makeConfig(rows, {drawHorizontalLine: () => {
+ return false;
+ }});
+
+ expect(config.drawHorizontalLine()).to.equal(false);
+ });
+ });
+ });
+
+ context('"singleLine', () => {
+ context('is not provided', () => {
+ it('defaults to retuning false', () => {
+ const config = makeConfig(rows);
+
expect(config.singleLine).to.equal(false);
});
});
+
+ context('is provided', () => {
+ it('uses the custom value', () => {
+ const config = makeConfig(rows, {singleLine: true});
+
+ expect(config.singleLine).to.equal(true);
+ });
+ });
});
});