jquery - streamlining a series of javascript functions -
is there better, more concise way this:
function gettweets(){ $.getjson("http://search.twitter.com/search.json?callback=?&q=superfad", function(data){ tweetsloaded = true; $.each(data.results, function(i,item){ var textplain = item.text; var textlinked = linkify(textplain); var texthashed = hashify(textlinked); var textlisted = listify(texthashed); function linkify(tweet){ return tweet.replace(/(http:\/\/[^\s]*)/g, "<a class='twtr-link' target=\"_blank\" href=\"$1\">$1</a>"); } function hashify(tweet){ return tweet.replace(/(^|\s+)#(\w+)/gi, function(m, before, hash) { return before + '<a target="_blank" class="twtr-hashtag" href="http://twitter.com/search?q=%23' + hash + '">#' + hash + '</a>'; }); } function listify(tweet) { return tweet.replace(/\b[@@]([a-za-z0-9_]{1,20})/g, function(m, username) { return '<a target="_blank" class="twtr-atreply" href="http://twitter.com/intent/user?screen_name=' + username + '">@' + username + '</a>'; }); } $("#twitter_results").append('<li class="twitter"><img class="twitter_img" src="' + item.profile_image_url + '"/>'+ textlisted + '</li>'); }); }); } //end gettweets
// define globally function stuffify(match, group1, group2) { switch (group1 || group2) { case 'http': return '<a class="twtr-link" target="_blank" href="' + match + '">' + match + '</a>'; case '#': return '<a class="twtr-hashtag" target="_blank" href="http://twitter.com/search?q=' + encodeuricomponent(match) + '">#' + match + '</a>' case '@': return '<a class="twtr-atreply" target="_blank" href="http://twitter.com/intent/user?screen_name=' + encodeuricomponent(match) + '">@' + match + '</a>'; default: return match; } } function(data){ tweetsloaded = true; var interestingparts = /(http):\/\/\s+|(#|@)[^\s.,!?;^()\[\]<>{}]+/g; $.each(data.results, function(i,item) { var newtext = item.text.replace(interestingparts, stuffify); $("#twitter_results").append('<li class="twitter"><img class="twitter_img" src="' + item.profile_image_url + '"/>'+ newtext + '</li>'); }); }
Comments
Post a Comment