Skip to content

Commit

Permalink
Fix space issues at the end of input string (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVerschueren authored and sindresorhus committed Jul 23, 2017
1 parent c0b4bfd commit 1bcb5fd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ const wrapWord = (rows, word, cols) => {
const exec = (str, cols, opts) => {
const options = opts || {};

if (str.trim() === '') {
return options.trim === false ? str : str.trim();
}

let pre = '';
let ret = '';
let escapeCode;
Expand Down
19 changes: 15 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ chalk.enabled = true;
const fixture = 'The quick brown ' + chalk.red('fox jumped over ') + 'the lazy ' + chalk.green('dog and then ran away with the unicorn.');
const fixture2 = '12345678\n901234567890';
const fixture3 = '12345678\n901234567890 12345';
const fixture4 = '12345678\n';
const fixture5 = '12345678\n ';

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

const res3 = m(fixture, 5, {wordWrap: false});
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');
const res3 = m(fixture5, 5, {wordWrap: false});
t.is(res3, '12345\n678\n');

const res4 = m(fixture, 5, {wordWrap: false});
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');
});

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

const res2 = m(fixture, 5, {wordWrap: false, trim: false});
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');
const res2 = m(fixture4, 5, {wordWrap: false, trim: false});
t.is(res2, '12345\n678\n');

const res3 = m(fixture5, 5, {wordWrap: false, trim: false});
t.is(res3, '12345\n678\n ');

const res4 = m(fixture, 5, {wordWrap: false, trim: false});
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');
});

test('supports fullwidth characters', t => {
Expand Down

0 comments on commit 1bcb5fd

Please sign in to comment.