Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
recursively break text into digestable tweets
/**
* 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