diff --git a/index.js b/index.js index b30f5ca..f419cff 100755 --- a/index.js +++ b/index.js @@ -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 = ''; @@ -83,8 +83,8 @@ const exec = (string, columns, options = {}) => { rows[rows.length - 1] = options.trim === false ? rows[rows.length - 1] : rows[rows.length - 1].trim(); 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; diff --git a/test.js b/test.js index 28b71f9..a7cd4f5 100755 --- a/test.js +++ b/test.js @@ -113,3 +113,25 @@ 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, whitespaces is properly wrapped', t => { + t.is(m(' ', 2, {trim: false}), ' \n '); + t.is(m(' ', 2, {trim: false, hard: true}), ' \n '); +}); + +test('#25, whitespace at the end of line is treated properly 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 leading space when line starts with ansi escape and 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')} `)); +});