Created
December 16, 2011 19:31
-
-
Save haldun/1487557 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
text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." | |
split_text = (text, max_line_size=30) -> | |
sum = (arr) -> | |
total = 0 | |
for el in arr | |
total += el | |
total | |
words = text.split(' ') | |
lines = [] | |
current_line = [] | |
for word in words | |
current_line_length = sum (w.length for w in current_line) | |
if word.length + current_line_length > max_line_size | |
lines.push current_line | |
current_line = [word] | |
else | |
current_line.push word | |
lines.push current_line if current_line.length? | |
lines | |
console.log split_text(text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment