Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix space issues at the end of input string #21

Merged
merged 1 commit into from
Jul 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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