Skip to content

Commit 1bcb5fd

Browse files
SamVerschuerensindresorhus
authored andcommittedJul 23, 2017
Fix space issues at the end of input string (#21)
1 parent c0b4bfd commit 1bcb5fd

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed
 

‎index.js

+4
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ const wrapWord = (rows, word, cols) => {
9999
const exec = (str, cols, opts) => {
100100
const options = opts || {};
101101

102+
if (str.trim() === '') {
103+
return options.trim === false ? str : str.trim();
104+
}
105+
102106
let pre = '';
103107
let ret = '';
104108
let escapeCode;

‎test.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ chalk.enabled = true;
1111
const fixture = 'The quick brown ' + chalk.red('fox jumped over ') + 'the lazy ' + chalk.green('dog and then ran away with the unicorn.');
1212
const fixture2 = '12345678\n901234567890';
1313
const fixture3 = '12345678\n901234567890 12345';
14+
const fixture4 = '12345678\n';
15+
const fixture5 = '12345678\n ';
1416

1517
test('wraps string at 20 characters', t => {
1618
const res20 = m(fixture, 20);
@@ -82,16 +84,25 @@ test('no word-wrapping', t => {
8284
const res2 = m(fixture3, 5, {wordWrap: false});
8385
t.is(res2, '12345\n678\n90123\n45678\n90 12\n345');
8486

85-
const res3 = m(fixture, 5, {wordWrap: false});
86-
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');
87+
const res3 = m(fixture5, 5, {wordWrap: false});
88+
t.is(res3, '12345\n678\n');
89+
90+
const res4 = m(fixture, 5, {wordWrap: false});
91+
t.is(res4, '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');
8792
});
8893

8994
test('no word-wrapping and no trimming', t => {
9095
const res = m(fixture3, 13, {wordWrap: false, trim: false});
9196
t.is(res, '12345678\n901234567890 \n12345');
9297

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');
98+
const res2 = m(fixture4, 5, {wordWrap: false, trim: false});
99+
t.is(res2, '12345\n678\n');
100+
101+
const res3 = m(fixture5, 5, {wordWrap: false, trim: false});
102+
t.is(res3, '12345\n678\n ');
103+
104+
const res4 = m(fixture, 5, {wordWrap: false, trim: false});
105+
t.is(res4, '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');
95106
});
96107

97108
test('supports fullwidth characters', t => {

0 commit comments

Comments
 (0)
Please sign in to comment.