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

Not inserting additional newlines when there is leading whitespace in a multiline string. #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,19 @@ module.exports = function(str, options) {

var re = new RegExp(regexString, 'g');
var lines = str.match(re) || [];
// The regex split will take whitespace from the start of next line,
// and put it on the end of the previous one. Need to move that back.
var extraWhitespace = '';
var result = indent + lines.map(function(line) {
if (line.slice(-1) === '\n') {
line = line.slice(0, line.length - 1);
var fullLine = extraWhitespace + line;
var match = line.match(/^(.+)\n([^\S\n]*)$/s);
if (match) {
fullLine = extraWhitespace + match[1];
extraWhitespace = match[2];
} else {
extraWhitespace = '';
}
return escape(line);
return escape(fullLine);
}).join(newline);

if (options.trim === true) {
Expand Down
14 changes: 14 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,18 @@ describe('wrap', function () {
' Supercalifragilistic\u200B\n expialidocious'
);
});

it('should preserve whitespace after a newline', function() {
assert.equal(
wrap('Highlights:\n First\n Second\n Third', { indent: '' }),
'Highlights:\n First\n Second\n Third'
);
});

it('should preserve whitespace after a newline and handle indents appropriately', function() {
assert.equal(
wrap('Highlights:\n First\n Second\n Third', { indent: '*' }),
'*Highlights:\n* First\n* Second\n* Third'
);
});
});