Skip to content

Instantly share code, notes, and snippets.

@meeech
Created February 16, 2011 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meeech/828919 to your computer and use it in GitHub Desktop.
Save meeech/828919 to your computer and use it in GitHub Desktop.
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