Created
December 16, 2011 19:32
-
-
Save haldun/1487559 to your computer and use it in GitHub Desktop.
split text into words
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
var split_text; | |
split_text = function(text, max_line_size) { | |
var current_line, current_line_length, lines, sum, w, word, words, _i, _len; | |
if (max_line_size == null) { | |
max_line_size = 30; | |
} | |
sum = function(arr) { | |
var el, total, _i, _len; | |
total = 0; | |
for (_i = 0, _len = arr.length; _i < _len; _i++) { | |
el = arr[_i]; | |
total += el; | |
} | |
return total; | |
}; | |
words = text.split(' '); | |
lines = []; | |
current_line = []; | |
for (_i = 0, _len = words.length; _i < _len; _i++) { | |
word = words[_i]; | |
current_line_length = sum((function() { | |
var _j, _len2, _results; | |
_results = []; | |
for (_j = 0, _len2 = current_line.length; _j < _len2; _j++) { | |
w = current_line[_j]; | |
_results.push(w.length); | |
} | |
return _results; | |
})()); | |
if (word.length + current_line_length > max_line_size) { | |
lines.push(current_line); | |
current_line = [word]; | |
} else { | |
current_line.push(word); | |
} | |
} | |
if (current_line.length != null) { | |
lines.push(current_line); | |
} | |
return lines; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment