Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function lineWrap(str) {
// Split by lines
return str.split(/\r|\n|\r\n/g).reduce(function(prev, piece) {
var parts = [];
// Wrap line
while (piece.length) {
var match = piece.match(/^.{1,80}(?:\s|$)/),
matchlen;
if (!match) {
match = [piece.substr(0, 80)];
}
if (matchlen = match[0].length) {
parts.push(match[0]);
piece = piece.substr(matchlen);
}
};
return prev.concat(parts);
}, []).join('\r\n');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.