Skip to content

Commit

Permalink
Fix a bunch of issues (#30)
Browse files Browse the repository at this point in the history
Fixes #23
Fixes #24
Fixes #25
Fixes #26
Fixes #27
  • Loading branch information
stroncium authored and sindresorhus committed Mar 3, 2019
1 parent 8640dc3 commit c084e3e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
19 changes: 12 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ const wrapWord = (rows, word, columns) => {
//
// 'soft' allows long words to expand past the column length
const exec = (string, columns, options = {}) => {
if (string.trim() === '') {
return options.trim === false ? string : string.trim();
if (options.trim !== false && string.trim() === '') {
return '';
}

let pre = '';
Expand All @@ -80,11 +80,13 @@ const exec = (string, columns, options = {}) => {
const rows = [''];

for (const [index, word] of string.split(' ').entries()) {
rows[rows.length - 1] = options.trim === false ? rows[rows.length - 1] : rows[rows.length - 1].trim();
if (options.trim !== false) {
rows[rows.length - 1] = rows[rows.length - 1].trimLeft();
}
let rowLength = stringWidth(rows[rows.length - 1]);

if (rowLength || word === '') {
if (rowLength === columns && options.wordWrap === false) {
if (index !== 0) {
if (rowLength === columns && (options.wordWrap === false || options.trim === false)) {
// If we start with a new word but the current row length equals the length of the columns, add a new row
rows.push('');
rowLength = 0;
Expand All @@ -96,8 +98,11 @@ const exec = (string, columns, options = {}) => {

// In 'hard' wrap mode, the length of a line is
// never allowed to extend past 'columns'
if (lengths[index] > columns && options.hard) {
if (rowLength) {
if (options.hard && lengths[index] > columns) {
const remainingColumns = (columns - rowLength);
const breaksStartingThisLine = 1 + Math.floor((lengths[index] - remainingColumns - 1) / columns);
const breaksStartingNextLine = Math.floor((lengths[index] - 1) / columns);
if (breaksStartingNextLine < breaksStartingThisLine) {
rows.push('');
}
wrapWord(rows, word, columns);
Expand Down
31 changes: 30 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ test('does not prepend newline if first string is greater than "cols"', t => {
test('breaks strings longer than "cols" characters', t => {
const res5 = m(fixture, 5, {hard: true});

t.is(res5, 'The\nquick\nbrown\n\u001B[31mfox\u001B[39m\n\u001B[31mjumpe\u001B[39m\n\u001B[31md\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.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));
});

Expand Down Expand Up @@ -113,3 +113,32 @@ 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');
});

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 ');
});

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 ');
});

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');
});

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 ');
});

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 '));
});

0 comments on commit c084e3e

Please sign in to comment.