# HG changeset patch # User hamidouk # Date 1322750608 -3600 # Node ID 138e76fe73a6fdfdf709c130c17fe244031beb2c # Parent 0e571ced5bc4e12e0b2643d82d5ab3642fdd32b3 extended the function to format tweet to apply styles to the polemic syntax. diff -r 0e571ced5bc4 -r 138e76fe73a6 src/js/utils.js --- a/src/js/utils.js Thu Dec 01 14:59:08 2011 +0100 +++ b/src/js/utils.js Thu Dec 01 15:43:28 2011 +0100 @@ -61,15 +61,26 @@ /* format a tweet - replaces @name by a link to the profile, #hashtag, etc. */ IriSP.formatTweet = function(tweet) { - var rNickname = /@(\w+)/gi; // matches a @handle - var rHashtag = /#(\w+)/gi; // matches a hashtag - var rUrl = /((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/gi; // copied from http://codegolf.stackexchange.com/questions/464/shortest-url-regex-match-in-javascript/480#480 + /* + an array of arrays which hold a regexp and its replacement. + */ + var regExps = [ + /* copied from http://codegolf.stackexchange.com/questions/464/shortest-url-regex-match-in-javascript/480#480 */ + [/((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/gi, "$1"], + [/@(\w+)/gi, "@$1"], // matches a @handle + [/#(\w+)/gi, "#$1"],// matches a hashtag + [/(\+\+)/gi, "$1"], + [/(--)/gi, "$1"], + [/(==)/gi, "$1"], + [/(\?\?)/gi, "$1"] + ]; + + var i = 0; + for(i = 0; i < regExps.length; i++) { + tweet = tweet.replace(regExps[i][0], regExps[i][1]); + } - var i1 = tweet.replace(rUrl, "$1"); - var i2 = i1.replace(rNickname, "@$1"); - var i3 = i2.replace(rHashtag, "#$1"); - - return i3; + return tweet; }; IriSP.countProperties = function(obj) { diff -r 0e571ced5bc4 -r 138e76fe73a6 unittests/tests/utils.js --- a/unittests/tests/utils.js Thu Dec 01 14:59:08 2011 +0100 +++ b/unittests/tests/utils.js Thu Dec 01 15:43:28 2011 +0100 @@ -42,9 +42,18 @@ }); test("test function to format a tweet", function() { - var input = "@handle @bundle #hashtag http://t.co/11111"; - var output = "@handle @bundle #hashtag http://t.co/11111"; - equal(IriSP.formatTweet(input), output, "the correct output is given"); + var inputs = ["@handle", "@bundle", "#hashtag", "http://t.co/11111", "++", "--"]; + var outputs = ["@handle", + "@bundle", + "#hashtag", + "http://t.co/11111", + "++", + "--"]; + + var i = 0; + for(i = 0; i < inputs.length; i++) { + equal(IriSP.formatTweet(inputs[i]), outputs[i], "the correct output is given"); + } }); test("test function to convert decimal color to hexadecimal", function() {