Skip to content

Commit

Permalink
Update dependencies and meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Mar 3, 2019
1 parent c084e3e commit cf78036
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 66 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const exec = (string, columns, options = {}) => {
if (options.trim !== false) {
rows[rows.length - 1] = rows[rows.length - 1].trimLeft();
}

let rowLength = stringWidth(rows[rows.length - 1]);

if (index !== 0) {
Expand All @@ -105,6 +106,7 @@ const exec = (string, columns, options = {}) => {
if (breaksStartingNextLine < breaksStartingThisLine) {
rows.push('');
}

wrapWord(rows, word, columns);
continue;
}
Expand Down
15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,15 @@
],
"dependencies": {
"ansi-styles": "^3.2.0",
"string-width": "^2.1.1",
"strip-ansi": "^4.0.0"
"string-width": "^3.0.0",
"strip-ansi": "^5.0.0"
},
"devDependencies": {
"ava": "^0.25.0",
"chalk": "^2.0.1",
"coveralls": "^3.0.0",
"ava": "^1.2.1",
"chalk": "^2.4.2",
"coveralls": "^3.0.3",
"has-ansi": "^3.0.0",
"nyc": "^13.0.1",
"strip-ansi": "^4.0.0",
"xo": "^0.22.0"
"nyc": "^13.3.0",
"xo": "^0.24.0"
}
}
114 changes: 56 additions & 58 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import test from 'ava';
import chalk from 'chalk';
import hasAnsi from 'has-ansi';
import stripAnsi from 'strip-ansi';
import m from '.';
import wrapAnsi from '.';

chalk.enabled = true;

Expand All @@ -15,130 +15,128 @@ const fixture4 = '12345678\n';
const fixture5 = '12345678\n ';

test('wraps string at 20 characters', t => {
const res20 = m(fixture, 20);
const result = wrapAnsi(fixture, 20);

t.is(res20, 'The quick brown \u001B[31mfox\u001B[39m\n\u001B[31mjumped over \u001B[39mthe lazy\n\u001B[32mdog and then ran\u001B[39m\n\u001B[32maway with the\u001B[39m\n\u001B[32municorn.\u001B[39m');
t.true(stripAnsi(res20).split('\n').every(x => x.length <= 20));
t.is(result, 'The quick brown \u001B[31mfox\u001B[39m\n\u001B[31mjumped over \u001B[39mthe lazy\n\u001B[32mdog and then ran\u001B[39m\n\u001B[32maway with the\u001B[39m\n\u001B[32municorn.\u001B[39m');
t.true(stripAnsi(result).split('\n').every(line => line.length <= 20));
});

test('wraps string at 30 characters', t => {
const res30 = m(fixture, 30);
const result = wrapAnsi(fixture, 30);

t.is(res30, 'The quick brown \u001B[31mfox jumped\u001B[39m\n\u001B[31mover \u001B[39mthe lazy \u001B[32mdog and then ran\u001B[39m\n\u001B[32maway with the unicorn.\u001B[39m');
t.true(stripAnsi(res30).split('\n').every(x => x.length <= 30));
t.is(result, 'The quick brown \u001B[31mfox jumped\u001B[39m\n\u001B[31mover \u001B[39mthe lazy \u001B[32mdog and then ran\u001B[39m\n\u001B[32maway with the unicorn.\u001B[39m');
t.true(stripAnsi(result).split('\n').every(line => line.length <= 30));
});

test('does not break strings longer than "cols" characters', t => {
const res5 = m(fixture, 5, {hard: false});
const result = wrapAnsi(fixture, 5, {hard: false});

t.is(res5, 'The\nquick\nbrown\n\u001B[31mfox\u001B[39m\n\u001B[31mjumped\u001B[39m\n\u001B[31mover\u001B[39m\n\u001B[31m\u001B[39mthe\nlazy\n\u001B[32mdog\u001B[39m\n\u001B[32mand\u001B[39m\n\u001B[32mthen\u001B[39m\n\u001B[32mran\u001B[39m\n\u001B[32maway\u001B[39m\n\u001B[32mwith\u001B[39m\n\u001B[32mthe\u001B[39m\n\u001B[32municorn.\u001B[39m');
t.true(stripAnsi(res5).split('\n').filter(x => x.length > 5).length > 0);
t.is(result, 'The\nquick\nbrown\n\u001B[31mfox\u001B[39m\n\u001B[31mjumped\u001B[39m\n\u001B[31mover\u001B[39m\n\u001B[31m\u001B[39mthe\nlazy\n\u001B[32mdog\u001B[39m\n\u001B[32mand\u001B[39m\n\u001B[32mthen\u001B[39m\n\u001B[32mran\u001B[39m\n\u001B[32maway\u001B[39m\n\u001B[32mwith\u001B[39m\n\u001B[32mthe\u001B[39m\n\u001B[32municorn.\u001B[39m');
t.true(stripAnsi(result).split('\n').filter(line => line.length > 5).length > 0);
});

test('handles colored string that wraps on to multiple lines', t => {
const res = m(chalk.green('hello world') + ' hey!', 5, {hard: false});
const lines = res.split('\n');
const result = wrapAnsi(chalk.green('hello world') + ' hey!', 5, {hard: false});
const lines = result.split('\n');
t.true(hasAnsi(lines[0]));
t.true(hasAnsi(lines[1]));
t.false(hasAnsi(lines[2]));
});

test('does not prepend newline if first string is greater than "cols"', t => {
const res = m(chalk.green('hello') + '-world', 5, {hard: false});
t.is(res.split('\n').length, 1);
const result = wrapAnsi(chalk.green('hello') + '-world', 5, {hard: false});
t.is(result.split('\n').length, 1);
});

// When "hard" is true

test('breaks strings longer than "cols" characters', t => {
const res5 = m(fixture, 5, {hard: true});
const result = wrapAnsi(fixture, 5, {hard: true});

t.is(res5, 'The\nquick\nbrown\n\u001B[31mfox j\u001B[39m\n\u001B[31mumped\u001B[39m\n\u001B[31mover\u001B[39m\n\u001B[31m\u001B[39mthe\nlazy\n\u001B[32mdog\u001B[39m\n\u001B[32mand\u001B[39m\n\u001B[32mthen\u001B[39m\n\u001B[32mran\u001B[39m\n\u001B[32maway\u001B[39m\n\u001B[32mwith\u001B[39m\n\u001B[32mthe\u001B[39m\n\u001B[32munico\u001B[39m\n\u001B[32mrn.\u001B[39m');
t.true(stripAnsi(res5).split('\n').every(x => x.length <= 5));
t.is(result, 'The\nquick\nbrown\n\u001B[31mfox j\u001B[39m\n\u001B[31mumped\u001B[39m\n\u001B[31mover\u001B[39m\n\u001B[31m\u001B[39mthe\nlazy\n\u001B[32mdog\u001B[39m\n\u001B[32mand\u001B[39m\n\u001B[32mthen\u001B[39m\n\u001B[32mran\u001B[39m\n\u001B[32maway\u001B[39m\n\u001B[32mwith\u001B[39m\n\u001B[32mthe\u001B[39m\n\u001B[32munico\u001B[39m\n\u001B[32mrn.\u001B[39m');
t.true(stripAnsi(result).split('\n').every(line => line.length <= 5));
});

test('removes last row if it contained only ansi escape codes', t => {
const res = m(chalk.green('helloworld'), 2, {hard: true});
t.true(stripAnsi(res).split('\n').every(x => x.length === 2));
const result = wrapAnsi(chalk.green('helloworld'), 2, {hard: true});
t.true(stripAnsi(result).split('\n').every(x => x.length === 2));
});

test('does not prepend newline if first word is split', t => {
const res = m(chalk.green('hello') + 'world', 5, {hard: true});
t.is(res.split('\n').length, 2);
const result = wrapAnsi(chalk.green('hello') + 'world', 5, {hard: true});
t.is(result.split('\n').length, 2);
});

test('takes into account line returns inside input', t => {
const res20 = m(fixture2, 10, {hard: true});
t.is(res20, '12345678\n9012345678\n90');
t.is(wrapAnsi(fixture2, 10, {hard: true}), '12345678\n9012345678\n90');
});

test('word wrapping', t => {
const res = m(fixture3, 15);
t.is(res, '12345678\n901234567890\n12345');
t.is(wrapAnsi(fixture3, 15), '12345678\n901234567890\n12345');
});

test('no word-wrapping', t => {
const res = m(fixture3, 15, {wordWrap: false});
t.is(res, '12345678\n901234567890 12\n345');
const result = wrapAnsi(fixture3, 15, {wordWrap: false});
t.is(result, '12345678\n901234567890 12\n345');

const res2 = m(fixture3, 5, {wordWrap: false});
t.is(res2, '12345\n678\n90123\n45678\n90 12\n345');
const result2 = wrapAnsi(fixture3, 5, {wordWrap: false});
t.is(result2, '12345\n678\n90123\n45678\n90 12\n345');

const res3 = m(fixture5, 5, {wordWrap: false});
t.is(res3, '12345\n678\n');
const rsult3 = wrapAnsi(fixture5, 5, {wordWrap: false});
t.is(rsult3, '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');
const result4 = wrapAnsi(fixture, 5, {wordWrap: false});
t.is(result4, '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 result = wrapAnsi(fixture3, 13, {wordWrap: false, trim: false});
t.is(result, '12345678\n901234567890 \n12345');

const res2 = m(fixture4, 5, {wordWrap: false, trim: false});
t.is(res2, '12345\n678\n');
const result2 = wrapAnsi(fixture4, 5, {wordWrap: false, trim: false});
t.is(result2, '12345\n678\n');

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

const res4 = m(fixture, 5, {wordWrap: false, trim: false});
t.is(res4, 'The q\nuick \nbrown\n \u001B[31mfox \u001B[39m\n[31mjumpe[39m\n[31md ove[39m\n[31mr \u001B[39mthe\n lazy\n \u001B[32mdog \u001B[39m\n[32mand t[39m\n[32mhen r[39m\n[32man aw[39m\n[32may wi[39m\n[32mth th[39m\n[32me uni[39m\n[32mcorn.\u001B[39m');
const result4 = wrapAnsi(fixture, 5, {wordWrap: false, trim: false});
t.is(result4, 'The q\nuick \nbrown\n \u001B[31mfox \u001B[39m\n[31mjumpe[39m\n[31md ove[39m\n[31mr \u001B[39mthe\n lazy\n \u001B[32mdog \u001B[39m\n[32mand t[39m\n[32mhen r[39m\n[32man aw[39m\n[32may wi[39m\n[32mth th[39m\n[32me uni[39m\n[32mcorn.\u001B[39m');
});

test('supports fullwidth characters', t => {
t.is(m('안녕하세', 4, {hard: true}), '안녕\n하세');
t.is(wrapAnsi('안녕하세', 4, {hard: true}), '안녕\n하세');
});

test('supports unicode surrogate pairs', t => {
t.is(m('a\uD83C\uDE00bc', 2, {hard: true}), 'a\n\uD83C\uDE00\nbc');
t.is(m('a\uD83C\uDE00bc\uD83C\uDE00d\uD83C\uDE00', 2, {hard: true}), 'a\n\uD83C\uDE00\nbc\n\uD83C\uDE00\nd\n\uD83C\uDE00');
t.is(wrapAnsi('a\uD83C\uDE00bc', 2, {hard: true}), 'a\n\uD83C\uDE00\nbc');
t.is(wrapAnsi('a\uD83C\uDE00bc\uD83C\uDE00d\uD83C\uDE00', 2, {hard: true}), 'a\n\uD83C\uDE00\nbc\n\uD83C\uDE00\nd\n\uD83C\uDE00');
});

test('#23, properly wraps whitespace with no trimming', t => {
t.is(m(' ', 2, {trim: false}), ' \n ');
t.is(m(' ', 2, {trim: false, hard: true}), ' \n ');
t.is(wrapAnsi(' ', 2, {trim: false}), ' \n ');
t.is(wrapAnsi(' ', 2, {trim: false, hard: true}), ' \n ');
});

test('#24, trims leading and trailing whitespace only on actual wrapped lines and only with trimming', t => {
t.is(m(' foo bar ', 6), 'foo\nbar');
t.is(m(' foo bar ', 42), 'foo bar');
t.is(m(' foo bar ', 42, {trim: false}), ' foo bar ');
t.is(wrapAnsi(' foo bar ', 6), 'foo\nbar');
t.is(wrapAnsi(' foo bar ', 42), 'foo bar');
t.is(wrapAnsi(' foo bar ', 42, {trim: false}), ' foo bar ');
});

test('#25, properly wraps whitespace between words with no trimming', t => {
t.is(m('foo bar', 3), 'foo\nbar');
t.is(m('foo bar', 3, {hard: true}), 'foo\nbar');
t.is(m('foo bar', 3, {trim: false}), 'foo\n \nbar');
t.is(m('foo bar', 3, {trim: false, hard: true}), 'foo\n \nbar');
t.is(wrapAnsi('foo bar', 3), 'foo\nbar');
t.is(wrapAnsi('foo bar', 3, {hard: true}), 'foo\nbar');
t.is(wrapAnsi('foo bar', 3, {trim: false}), 'foo\n \nbar');
t.is(wrapAnsi('foo bar', 3, {trim: false, hard: true}), 'foo\n \nbar');
});

test('#26, does not multiplicate leading spaces with no trimming', t => {
t.is(m(' a ', 10, {trim: false}), ' a ');
t.is(m(' a ', 10, {trim: false}), ' a ');
t.is(wrapAnsi(' a ', 10, {trim: false}), ' a ');
t.is(wrapAnsi(' a ', 10, {trim: false}), ' a ');
});

test('#27, does not remove spaces in line with ansi escapes when no trimming', t => {
t.is(m(chalk.bgGreen(` ${chalk.black('OK')} `), 100, {trim: false}), chalk.bgGreen(` ${chalk.black('OK')} `));
t.is(m(chalk.bgGreen(` ${chalk.black('OK')} `), 100, {trim: false}), chalk.bgGreen(` ${chalk.black('OK')} `));
t.is(m(chalk.bgGreen(' hello '), 10, {hard: true, trim: false}), chalk.bgGreen(' hello '));
t.is(wrapAnsi(chalk.bgGreen(` ${chalk.black('OK')} `), 100, {trim: false}), chalk.bgGreen(` ${chalk.black('OK')} `));
t.is(wrapAnsi(chalk.bgGreen(` ${chalk.black('OK')} `), 100, {trim: false}), chalk.bgGreen(` ${chalk.black('OK')} `));
t.is(wrapAnsi(chalk.bgGreen(' hello '), 10, {hard: true, trim: false}), chalk.bgGreen(' hello '));
});

0 comments on commit cf78036

Please sign in to comment.