extended the function to format tweet to apply styles to the polemic syntax. popcorn-port
authorhamidouk
Thu, 01 Dec 2011 15:43:28 +0100
branchpopcorn-port
changeset 374 138e76fe73a6
parent 373 0e571ced5bc4
child 375 8c41db3813e0
extended the function to format tweet to apply styles to the polemic syntax.
src/js/utils.js
unittests/tests/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, "<a href='$1'>$1</a>"],
+    [/@(\w+)/gi, "<a href='http://twitter.com/$1'>@$1</a>"], // matches a @handle
+    [/#(\w+)/gi, "<a href='http://twitter.com/search?q=%23$1'>#$1</a>"],// matches a hashtag
+    [/(\+\+)/gi, "<span class='Ldt-PolemicPlusPlus'>$1</span>"],
+    [/(--)/gi, "<span class='Ldt-PolemicMinusMinus'>$1</span>"],
+    [/(==)/gi, "<span class='Ldt-PolemicEqualEqual'>$1</span>"],
+    [/(\?\?)/gi, "<span class='Ldt-PolemicQuestion'>$1</span>"]
+  ]; 
+
+  var i = 0;
+  for(i = 0; i < regExps.length; i++) {
+     tweet = tweet.replace(regExps[i][0], regExps[i][1]);
+  }
   
-  var i1 = tweet.replace(rUrl, "<a href='$1'>$1</a>");
-  var i2 = i1.replace(rNickname, "<a href='http://twitter.com/$1'>@$1</a>");
-  var i3 = i2.replace(rHashtag, "<a href='http://twitter.com/search?q=%23$1'>#$1</a>");
-
-  return i3;
+  return tweet;
 };
 
 IriSP.countProperties = function(obj) {
--- 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 = "<a href='http://twitter.com/handle'>@handle</a> <a href='http://twitter.com/bundle'>@bundle</a> <a href='http://twitter.com/search?q=%23hashtag'>#hashtag</a> <a href='http://t.co/11111'>http://t.co/11111</a>";
-    equal(IriSP.formatTweet(input), output, "the correct output is given");
+    var inputs = ["@handle", "@bundle", "#hashtag", "http://t.co/11111", "++", "--"];
+    var outputs = ["<a href='http://twitter.com/handle'>@handle</a>", 
+                   "<a href='http://twitter.com/bundle'>@bundle</a>",
+                   "<a href='http://twitter.com/search?q=%23hashtag'>#hashtag</a>",
+                   "<a href='http://t.co/11111'>http://t.co/11111</a>",
+                   "<span class='Ldt-PolemicPlusPlus'>++</span>",
+                   "<span class='Ldt-PolemicMinusMinus'>--</span>"];
+
+    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() {