Skip to content

Instantly share code, notes, and snippets.

@namuol
Created May 1, 2012 04:59
Show Gist options
  • Save namuol/2565206 to your computer and use it in GitHub Desktop.
Save namuol/2565206 to your computer and use it in GitHub Desktop.
function split_to_lines(str, max_width) {
var current_line, lines, word, words, _i, _len;
lines = [];
words = str.split(' ');
current_line = '';
for (_i = 0, _len = words.length; _i < _len; _i++) {
word = words[_i];
if (current_line.length + word.length > max_width) {
lines.push(current_line);
current_line = word + ' ';
} else {
current_line += word + ' ';
}
}
lines.push(current_line);
return lines;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment