Skip to content

Commit a8c3423

Browse files
SamVerschuerensindresorhus
authored andcommittedJul 23, 2017
Add option to not remove whitespace - fixes #9 (#17)
1 parent f16f943 commit a8c3423

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed
 

‎index.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,16 @@ const exec = (str, cols, opts) => {
111111
const i = item[0];
112112
const word = item[1];
113113

114+
rows[rows.length - 1] = options.trim === false ? rows[rows.length - 1] : rows[rows.length - 1].trim();
114115
let rowLength = stringWidth(rows[rows.length - 1]);
115116

116-
if (rowLength) {
117+
if (rowLength || word === '') {
118+
if (rowLength === cols && options.wordWrap === false) {
119+
// If we start with a new word but the current row length equals the length of the columns, add a new row
120+
rows.push('');
121+
rowLength = 0;
122+
}
123+
117124
rows[rows.length - 1] += ' ';
118125
rowLength++;
119126
}
@@ -145,7 +152,7 @@ const exec = (str, cols, opts) => {
145152
rows[rows.length - 1] += word;
146153
}
147154

148-
pre = rows.map(x => x.trim()).join('\n');
155+
pre = rows.map(r => options.trim === false ? r : r.trim()).join('\n');
149156

150157
for (const item of Array.from(pre).entries()) {
151158
const i = item[0];

‎readme.md

+7
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ Default: `true`
6161

6262
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.
6363

64+
##### trim
65+
66+
Type: `boolean`<br>
67+
Default: `true`
68+
69+
Whitespace on all lines is removed by default. Set this option to `false` if you don't want to trim.
70+
6471

6572
## Related
6673

‎test.js

+8
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ test('no word-wrapping', t => {
8686
t.is(res3, 'The q\nuick\nbrown\n\u001B[31mfox j\u001B[39m\n\u001B[31mumped\u001B[39m\n\u001B[31mover\u001B[39m\n\u001B[31m\u001B[39mthe l\nazy \u001B[32md\u001B[39m\n\u001B[32mog an\u001B[39m\n\u001B[32md the\u001B[39m\n\u001B[32mn ran\u001B[39m\n\u001B[32maway\u001B[39m\n\u001B[32mwith\u001B[39m\n\u001B[32mthe u\u001B[39m\n\u001B[32mnicor\u001B[39m\n\u001B[32mn.\u001B[39m');
8787
});
8888

89+
test('no word-wrapping and no trimming', t => {
90+
const res = m(fixture3, 13, {wordWrap: false, trim: false});
91+
t.is(res, '12345678\n901234567890 \n12345');
92+
93+
const res2 = m(fixture, 5, {wordWrap: false, trim: false});
94+
t.is(res2, 'The q\nuick \nbrown\n \u001B[31mfox \u001B[39m\njumpe\nd ove\nr \u001B[39mthe\n lazy\n \u001B[32mdog \u001B[39m\nand t\nhen r\nan aw\nay wi\nth th\ne uni\ncorn.\u001B[39m');
95+
});
96+
8997
test('supports fullwidth characters', t => {
9098
t.is(m('안녕하세', 4, {hard: true}), '안녕\n하세');
9199
});

0 commit comments

Comments
 (0)
Please sign in to comment.