Skip to content

Instantly share code, notes, and snippets.

@gartenfeld
Created September 14, 2015 16:58
Show Gist options
  • Save gartenfeld/2dca7537b61f456e20b2 to your computer and use it in GitHub Desktop.
Save gartenfeld/2dca7537b61f456e20b2 to your computer and use it in GitHub Desktop.
Wrapping a long string into a paragraph with line-breaks.
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