Skip to content

Commit

Permalink
Merge pull request #41 from jonschlinkert/fix/CVE-2023-26115-2
Browse files Browse the repository at this point in the history
🔒fix: CVE 2023 26115 (2)
  • Loading branch information
doowb authored Jul 18, 2023
2 parents 786ebf1 + ace0b3c commit 420dce9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
19 changes: 17 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
/*!
* word-wrap <https://github.com/jonschlinkert/word-wrap>
*
* Copyright (c) 2014-2017, Jon Schlinkert.
* Copyright (c) 2014-2023, Jon Schlinkert.
* Released under the MIT License.
*/

function trimEnd(str) {
let lastCharPos = str.length - 1;
let lastChar = str[lastCharPos];
while(lastChar === ' ' || lastChar === '\t') {
lastChar = str[--lastCharPos];
}
return str.substring(0, lastCharPos + 1);
}

function trimTabAndSpaces(str) {
const lines = str.split('\n');
const trimmedLines = lines.map((line) => trimEnd(line));
return trimmedLines.join('\n');
}

module.exports = function(str, options) {
options = options || {};
if (str == null) {
Expand Down Expand Up @@ -36,7 +51,7 @@ module.exports = function(str, options) {
}).join(newline);

if (options.trim === true) {
result = result.replace(/[ \t]*$/gm, '');
result = trimTabAndSpaces(result);
}
return result;
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "word-wrap",
"description": "Wrap words to a specified length.",
"version": "1.2.3",
"version": "1.2.4",
"homepage": "https://github.com/jonschlinkert/word-wrap",
"author": "Jon Schlinkert (https://github.com/jonschlinkert)",
"contributors": [
Expand Down
4 changes: 4 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ describe('wrap', function () {
assert.equal(wrap(str, {trim: true}), 'A project without documentation is like a project\nthat doesn\'t exist. Verb solves this by making it\ndead simple to generate project documentation,\nusing simple markdown templates, with zero\nconfiguration required.');
});

it('should trim trailing whitespace (even for empty lines):', function () {
assert.equal(wrap("a \n\nb \n \nc\t", {trim: true}), 'a\n\nb\n\nc');
});

it('should handle strings with just newlines', function () {
assert.equal(wrap('\r\n', {indent: '\r\n', width: 18}), '\r\n');
});
Expand Down

0 comments on commit 420dce9

Please sign in to comment.