Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,12 @@ function textWrap(maxLength, input) {
function pushLine(str, ws) {
if (line.length && ws) line += ws;
line += str;
while (line.length > maxLength) {
lines.push(line.slice(0, maxLength));
line = line.slice(maxLength);
while (strlen(line) > maxLength) {
let head = truncateWidth(line, maxLength);
// a full-width char cannot fit in a single column, take it anyway so the loop makes progress
if (!head.length) head = line.slice(0, 1);
lines.push(head);
line = line.slice(head.length);
}
}
let split = input.split(/(\s+)/g);
Expand Down
20 changes: 20 additions & 0 deletions test/table-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@ describe('@api Table ', function () {
expect(table.toString()).toEqual(expected.join('\n'));
});

it('wordWrap without word boundaries does not drop full-width chars', function () {
let table = new Table({ colWidths: [14], wordWrap: true, wrapOnWordBoundary: false });

table.push(['가나다라마바사아자차카타파하']);

let expected = ['┌──────────────┐', '│ 가나다라마바 │', '│ 사아자차카타 │', '│ 파하 │', '└──────────────┘'];

expect(table.toString()).toEqual(expected.join('\n'));
});

it('wordWrap without word boundaries keeps ascii behaviour', function () {
let table = new Table({ colWidths: [14], wordWrap: true, wrapOnWordBoundary: false });

table.push(['abcdefghijklmnopqrstuvwx']);

let expected = ['┌──────────────┐', '│ abcdefghijkl │', '│ mnopqrstuvwx │', '└──────────────┘'];

expect(table.toString()).toEqual(expected.join('\n'));
});

it('allows numbers as `content` property of cells defined using object notation', function () {
let table = new Table({ style: { border: [], head: [] } });

Expand Down
13 changes: 13 additions & 0 deletions test/utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,19 @@ describe('utils', function () {
'stuv wx yz',
]);
});

it('wraps long full-width words by display width', function () {
// 12 columns fit 6 full-width chars, not 12
expect(wordWrap(12, '가나다라마바사아자차카타파하', false)).toEqual(['가나다라마바', '사아자차카타', '파하']);
expect(wordWrap(12, '这是一段没有空格的中文', false)).toEqual(['这是一段没有', '空格的中文']);
});

it('never returns a line wider than maxLength', function () {
let lines = wordWrap(12, '가나다라마바사아자차카타파하', false);
lines.forEach(function (line) {
expect(strlen(line)).toBeLessThanOrEqual(12);
});
});
});
});

Expand Down