Skip to content

Instantly share code, notes, and snippets.

@indutny
Created January 15, 2011 17:00
Show Gist options
  • Save indutny/781046 to your computer and use it in GitHub Desktop.
Save indutny/781046 to your computer and use it in GitHub Desktop.
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