Created
February 16, 2011 05:29
-
-
Save meeech/828919 to your computer and use it in GitHub Desktop.
recursively break text into digestable tweets
This file contains 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
/** | |
* finds the index to substr on. looks for space from end of string | |
* @param string | |
* @return int | |
*/ | |
var calculateIndex = function(value) { | |
var index = value.substr(0,TWEET_SIZE).lastIndexOf(" "); | |
if (index < 1 || value.length < TWEET_SIZE) { | |
index = TWEET_SIZE; | |
} | |
return index; | |
}; | |
/** | |
* processes the tweet, recursive, pass in the array to collect the pieces | |
* @param string value The text to break up. | |
* | |
* @return array | |
*/ | |
var process = function(value, collector) { | |
collector = collector || []; | |
if(1 > value.length){ | |
//Fix elipsis | |
Y.Array.each(collector, function(v,i,a) { | |
if(a.length != (i+1)) { | |
a[i] = v + CONTINUE_MARKER; | |
} | |
}); | |
return collector; | |
} | |
var index = calculateIndex(value); | |
collector.push(value.substr(0,index)); | |
return process(value.substr(index), collector); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment