Skip to content

Commit ebe2a2f

Browse files
authored
feat: ability to override wordWrap and wrapOnBoundary per cell (#303)
1 parent 450f957 commit ebe2a2f

File tree

2 files changed

+81
-4
lines changed

2 files changed

+81
-4
lines changed

src/cell.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ class Cell {
7373
}
7474

7575
computeLines(tableOptions) {
76-
if (this.fixedWidth && (tableOptions.wordWrap || tableOptions.textWrap)) {
76+
const tableWordWrap = tableOptions.wordWrap || tableOptions.textWrap;
77+
const { wordWrap = tableWordWrap } = this.options;
78+
if (this.fixedWidth && wordWrap) {
7779
this.fixedWidth -= this.paddingLeft + this.paddingRight;
7880
if (this.colSpan) {
7981
let i = 1;
@@ -82,7 +84,8 @@ class Cell {
8284
i++;
8385
}
8486
}
85-
const { wrapOnWordBoundary = true } = tableOptions;
87+
const { wrapOnWordBoundary: tableWrapOnWordBoundary = true } = tableOptions;
88+
const { wrapOnWordBoundary = tableWrapOnWordBoundary } = this.options;
8689
return this.wrapLines(utils.wordWrap(this.fixedWidth, this.content, wrapOnWordBoundary));
8790
}
8891
return this.wrapLines(this.content.split('\n'));

test/cell-test.js

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,22 @@ describe('Cell', function () {
22
const colors = require('@colors/colors');
33
const Cell = require('../src/cell');
44
const { ColSpanCell, RowSpanCell } = Cell;
5-
const { mergeOptions } = require('../src/utils');
5+
const utils = require('../src/utils');
6+
const { wordWrap: actualWordWrap } = jest.requireActual('../src/utils');
7+
8+
jest.mock('../src/utils', () => ({
9+
...jest.requireActual('../src/utils'),
10+
wordWrap: jest.fn(), // only mock this
11+
}));
12+
13+
beforeEach(() => {
14+
// use the default implementation, unless we override
15+
utils.wordWrap.mockImplementation((...args) => actualWordWrap(...args));
16+
});
617

718
function defaultOptions() {
819
//overwrite coloring of head and border by default for easier testing.
9-
return mergeOptions({ style: { head: [], border: [] } });
20+
return utils.mergeOptions({ style: { head: [], border: [] } });
1021
}
1122

1223
function defaultChars() {
@@ -89,6 +100,69 @@ describe('Cell', function () {
89100
});
90101

91102
describe('mergeTableOptions', function () {
103+
describe('wordwrap', function () {
104+
let tableOptions, cell;
105+
106+
function initCell({ wordWrap, wrapOnWordBoundary } = {}) {
107+
cell = new Cell({ content: 'some text', wordWrap, wrapOnWordBoundary });
108+
cell.x = cell.y = 0;
109+
}
110+
111+
beforeEach(() => {
112+
utils.wordWrap.mockClear();
113+
tableOptions = { ...defaultOptions(), colWidths: [50] };
114+
});
115+
116+
it('no cell wordWrap override (tableOptions.wordWrap=true)', function () {
117+
tableOptions.wordWrap = true; // wrapOnWordBoundary is true by default
118+
initCell();
119+
120+
cell.mergeTableOptions(tableOptions);
121+
expect(utils.wordWrap).toHaveBeenCalledWith(expect.any(Number), cell.content, true /*wrapOnWordBoundary*/);
122+
});
123+
124+
it('no cell wordWrap override (tableOptions.wordWrap=false)', function () {
125+
tableOptions.wordWrap = false; // wrapOnWordBoundary is true by default
126+
initCell();
127+
128+
cell.mergeTableOptions(tableOptions);
129+
expect(utils.wordWrap).not.toHaveBeenCalled();
130+
});
131+
132+
it('cell wordWrap override (cell.options.wordWrap=false)', function () {
133+
tableOptions.wordWrap = true; // wrapOnWordBoundary is true by default
134+
initCell({ wordWrap: false });
135+
136+
cell.mergeTableOptions(tableOptions);
137+
expect(utils.wordWrap).not.toHaveBeenCalled(); // no wrapping done
138+
});
139+
140+
it('cell wordWrap override (cell.options.wordWrap=true)', function () {
141+
tableOptions.wordWrap = false; // wrapOnWordBoundary is true by default
142+
initCell({ wordWrap: true });
143+
144+
cell.mergeTableOptions(tableOptions);
145+
expect(utils.wordWrap).toHaveBeenCalled();
146+
});
147+
148+
it('cell wrapOnWordBoundary override (cell.options.wrapOnWordBoundary=false)', function () {
149+
tableOptions.wordWrap = true; // wrapOnWordBoundary is true by default
150+
initCell({ wrapOnWordBoundary: false });
151+
152+
cell.mergeTableOptions(tableOptions);
153+
expect(utils.wordWrap).toHaveBeenCalledWith(expect.any(Number), cell.content, false /*wrapOnWordBoundary*/);
154+
});
155+
156+
it('cell wrapOnWordBoundary override (cell.options.wrapOnWordBoundary=true)', function () {
157+
tableOptions.wordWrap = true;
158+
tableOptions.wrapOnWordBoundary = false;
159+
initCell({ wrapOnWordBoundary: true });
160+
161+
cell.mergeTableOptions(tableOptions);
162+
expect(utils.wordWrap).toHaveBeenCalledWith(expect.any(Number), cell.content, true /*wrapOnWordBoundary*/);
163+
});
164+
});
165+
92166
describe('chars', function () {
93167
it('unset chars take on value of table', function () {
94168
let cell = new Cell();

0 commit comments

Comments
 (0)