Created
January 15, 2011 17:00
-
-
Save indutny/781046 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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