Skip to content

Commit

Permalink
add option to not remove whitespace - fixes #9
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVerschueren committed Jun 13, 2017
1 parent e910f82 commit 73b16f1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function exec(str, cols, opts) {
for (var i = 0, word; (word = words[i]) !== undefined; i++) {
var rowLength = stringWidth(rows[rows.length - 1]);

if (rowLength) {
if (rowLength || word === '') {
rows[rows.length - 1] += ' ';
rowLength++;
}
Expand Down Expand Up @@ -135,7 +135,7 @@ function exec(str, cols, opts) {
}

pre = rows.map(function (r) {
return r.trim();
return options.trim === false ? r : r.trim();
}).join('\n');

for (var j = 0; j < pre.length; j++) {
Expand Down
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ Default: `true`

By default, an attempt is made to split words at spaces, ensuring that they don't extend past the configured columns. If wordWrap is `false`, each column will instead be completely filled splitting words as necessary.

##### trim

Type: `boolean`<br>
Default: `true`

Whitespace on all lines is removed by default. Set this option to `false` if you don't want to trim.


## Related

Expand Down
8 changes: 8 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ test('no word-wrapping', t => {
t.is(res2, 'The q\nuick\nbrown\nfox j\numped\nover\nthe l\nazy d\nog an\nd the\nn ran\naway\nwith\nthe u\nnicor\nn.');
});

test('no word-wrapping and no trimming', t => {
const res = fn(fixture3, 10, {wordWrap: false, trim: false});
t.is(res, '12345678\n901234567890 \n12345');

const res2 = fn(fixture, 5, {wordWrap: false, trim: false});
t.is(res2, 'The q\nuick \nbrown \nfox j\numped \nover \nthe l\nazy d\nog an\nd the\nn ran \naway \nwith \nthe u\nnicor\nn.');
});

// https://github.com/chalk/wrap-ansi/issues/10
test.failing('supports fullwidth characters', t => {
t.is(fn('안녕하세', 4, {hard: true}), '안녕\n하세');
Expand Down

0 comments on commit 73b16f1

Please sign in to comment.