Created
September 14, 2015 16:58
-
-
Save gartenfeld/2dca7537b61f456e20b2 to your computer and use it in GitHub Desktop.
Wrapping a long string into a paragraph with line-breaks.
This file contains hidden or 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 wrap (text, limit) { | |
if (text.length > limit) { | |
// find the last space within limit | |
var edge = text.slice(0, limit).lastIndexOf(' '); | |
if (edge > 0) { | |
var line = text.slice(0, edge); | |
var remainder = text.slice(edge + 1); | |
return line + '\n' + wrap(remainder, limit); | |
} | |
} | |
return text; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment