You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2011/05/16 20:02:52 UTC

svn commit: r1103816 - in /tapestry/tapestry5/trunk/tapestry-core/src: main/resources/org/apache/tapestry5/t5-arrays.js main/resources/org/apache/tapestry5/t5-pubsub.js test/resources/org/apache/tapestry5/integration/app1/pages/JavaScriptTests.tml

Author: hlship
Date: Mon May 16 18:02:52 2011
New Revision: 1103816

URL: http://svn.apache.org/viewvc?rev=1103816&view=rev
Log:
TAP5-999: Change the order of parameters of map(), mapcat(), etc. to have the function first and the array last

Modified:
    tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-arrays.js
    tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-pubsub.js
    tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/JavaScriptTests.tml

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-arrays.js
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-arrays.js?rev=1103816&r1=1103815&r2=1103816&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-arrays.js (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-arrays.js Mon May 16 18:02:52 2011
@@ -28,13 +28,13 @@ T5.extend(T5, function() {
 	/**
 	 * Iterates over an array, invoking a function for each array element.
 	 * 
-	 * @param array
-	 *            to iterate over (possibly null or undefined)
 	 * @param fn
 	 *            passed each element in array as first parameter, element index
 	 *            as second parameter
+	 * @param array
+	 *            to iterate over (possibly null or undefined)
 	 */
-	function each(array, fn) {
+	function each(fn, array) {
 		if (isNonEmpty(array)) {
 			for ( var index = 0; index < array.length; index++) {
 				fn(array[index], index);
@@ -46,19 +46,19 @@ T5.extend(T5, function() {
 	 * Maps over a JavaScript array, passing each value to the mapper function.
 	 * Returns the array of return values from the mapper.
 	 * 
-	 * @param array
-	 *            object to iterate over (may be null or undefined)
 	 * @param mapperfn
 	 *            function passed each object from the array, and the index for
 	 *            each object from the array
+	 * @param array
+	 *            object to iterate over (may be null or undefined)
 	 * @returns result array (possibly empty)
 	 */
-	function map(array, mapperfn) {
+	function map(mapperfn, array) {
 		var result = [];
 
-		each(array, function(element, index) {
+		each(function(element, index) {
 			result[index] = mapperfn(element, index);
-		});
+		}, array);
 
 		return result;
 	}
@@ -69,40 +69,40 @@ T5.extend(T5, function() {
 	 * reducer function with the second element, and so on. The final result is
 	 * the accumulator after all elements have been passed.
 	 * 
-	 * @param array
-	 *            array (may be null or undefined)
-	 * @param initial
-	 *            the initial value for the accumulator
 	 * @param reducerfn
 	 *            passed the accumulator, an element, and an index and returns
 	 *            the new accumulator
+	 * @param initial
+	 *            the initial value for the accumulator
+	 * @param array
+	 *            array (may be null or undefined)
 	 * @returns the accumulator
 	 */
-	function reduce(array, initial, reducerfn) {
+	function reduce(reducerfn, initial, array) {
 		var accumulator = initial;
 
-		each(array, function(element, index) {
+		each(function(element, index) {
 			accumulator = reducerfn(accumulator, element, index);
-		});
+		}, array);
 
 		return accumulator;
 	}
 
 	var concat = Array.prototype.concat;
-	
+
 	/**
 	 * A variation of map, where the mapperfn is expected to return an array of
 	 * values (not a single value). The result arrays are concatenated, to
 	 * return a single flattened result.
 	 * 
-	 * @param array
-	 *            to iterate over
 	 * @param mapperfn
 	 *            passed each element and index, returns an array of results
-	 * @returns the concatination of the result arrays
+	 * @param array
+	 *            to iterate over
+	 * @returns the concatenation of the result arrays
 	 */
-	function mapcat(array, mapperfn) {
-		var results = map(array, mapperfn);
+	function mapcat(mapperfn, array) {
+		var results = map(mapperfn, array);
 
 		return concat.apply([], results);
 	}
@@ -113,13 +113,13 @@ T5.extend(T5, function() {
 	 * comparison. May return the original array unchanged if the element is not
 	 * present.
 	 * 
-	 * @param array
-	 *            a non-null array
 	 * @param element
 	 *            to remove from array
+	 * @param array
+	 *            a non-null array
 	 * @returns the array, or the array with any references to element removed
 	 */
-	function without(array, element) {
+	function without(element, array) {
 		var index;
 		for (index = array.length - 1; index >= 0; index--) {
 			if (array[index] === element) {

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-pubsub.js
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-pubsub.js?rev=1103816&r1=1103815&r2=1103816&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-pubsub.js (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/t5-pubsub.js Mon May 16 18:02:52 2011
@@ -17,7 +17,6 @@ T5.extend(T5, function() {
 
 	var map = T5.map;
 	var mapcat = T5.mapcat;
-	var each = T5.each;
 
 	var subscribersVersion = 0;
 
@@ -47,9 +46,9 @@ T5.extend(T5, function() {
 
 	function doPublish(listeners, message) {
 
-		return map(listeners, function(fn) {
+		return map(function(fn) {
 			fn(message);
-		});
+		}, listeners);
 	}
 
 	/**
@@ -82,9 +81,9 @@ T5.extend(T5, function() {
 				// has changed.
 
 				if (subscribersVersionSnapshot !== subscribersVersion) {
-					listeners = mapcat(selectors, function(selector) {
+					listeners = mapcat(function(selector) {
 						return subscribers[selector] || [];
-					});
+					}, selectors);
 
 					subscribersVersionSnapshot = subscribersVersion;
 				}
@@ -111,7 +110,7 @@ T5.extend(T5, function() {
 	function unsubscribe(selector, listenerfn) {
 		var listeners = subscribers[selector];
 
-		var editted = T5.without(listeners, listenerfn);
+		var editted = T5.without(listenerfn, listeners);
 
 		if (editted !== listeners) {
 			subscribers[selector] = editted;

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/JavaScriptTests.tml
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/JavaScriptTests.tml?rev=1103816&r1=1103815&r2=1103816&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/JavaScriptTests.tml (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/JavaScriptTests.tml Mon May 16 18:02:52 2011
@@ -18,7 +18,7 @@
 
     <tr>
       <td>
-        assertEqual(map(undefined, null), [])
+        assertEqual(map(null, undefined), [])
     </td>
       <td>
         Map() on undefined returns an empty array
@@ -27,14 +27,14 @@
 
     <tr>
       <td>
-        assertEqual(map([], null), [])
+        assertEqual(map(null, []), [])
     </td>
       <td>Map() on empty array returns an empty array</td>
     </tr>
 
     <tr>
       <td>
-        assertEqual(map([1, 2, 3], function(e) { return e + 100; }), [101, 102, 103])
+        assertEqual(map(function(e) { return e + 100; }, [1, 2, 3]), [101, 102, 103])
         </td>
       <td>
         The normal use of map().
@@ -44,8 +44,7 @@
     <tr>
       <td>
         <pre>
-          assertEqual(map([1, 2, 3],
-          function(e, index) { return (e + 100) + "@" + index; }),
+          assertEqual(map(function(e, index) { return (e + 100) + "@" + index; }, [1, 2, 3]),
           ["101@0", "102@1",
           "103@2"])            
         </pre>
@@ -59,7 +58,7 @@
       <td>
         <pre>
           var initial = { };
-          assertSame(reduce([], initial, function() { }), initial);
+          assertSame(reduce(function() { }, initial, []), initial);
         </pre>
       </td>
       <td>
@@ -71,12 +70,12 @@
 
       <td>
         <pre>
-          assertEqual(reduce(["fred", "barney", "wilma"], {},
-          function(o, value, index) {
+          assertEqual(reduce(function(o, value, index) {
           o[value] = index;
 
           return o;
-          }),
+          }, {}, ["fred", "barney",
+          "wilma"]),
           { fred: 0, barney: 1, wilma: 2 })
         </pre>
       </td>
@@ -88,7 +87,7 @@
       <td>
         <pre>
           var initial = [1, 2, 3]
-          assertSame(without(initial, 9), initial)
+          assertSame(without(9, initial), initial)
         </pre>
       </td>
       <td>
@@ -98,7 +97,7 @@
 
     <tr>
       <td>
-        assertEqual(without([1, 2, 2, 3, 4, 2, 5], 2), [1, 3, 4, 5])
+        assertEqual(without(2, [1, 2, 2, 3, 4, 2, 5]), [1, 3, 4, 5])
         </td>
       <td>
         Multiple matches should all be removed
@@ -109,7 +108,7 @@
       <td>
         <pre>
           var initial = [1, 2, 3]
-          without(initial, 2)
+          without(2, initial)
           assertEqual(initial, [1, 2, 3])
           </pre>
       </td>
@@ -126,6 +125,22 @@
     <tr>
       <td>
         <pre>
+          assertEqual(mapcat(function (value) {
+          var out = []
+          for (var i = 0; i &lt; value; i++) { out.push(value); }
+          return out;
+          }, [1, 3, 5]),
+          [1, 3, 3, 3, 5, 5, 5, 5, 5])            
+            </pre>
+      </td>
+      <td>
+        Basic execution of mapcat().
+      </td>
+    </tr>
+
+    <tr>
+      <td>
+        <pre>
           var message = { }
           var rcvd;
 
@@ -159,7 +174,8 @@
 
           unsub()
 
-          pub("foo"); assertEqual(count, 2)
+          pub("foo");
+          assertEqual(count, 2)
           </pre>
       </td>
       <td>
@@ -173,7 +189,8 @@
 
           sub("foo/bar", function() { pubs.push("foo/bar"); })
           sub("foo", function() {
-          pubs.push("foo"); })
+          pubs.push("foo");
+          })
 
           pub("foo/bar")
 
@@ -192,7 +209,8 @@
 
           sub("foo/bar", function() { pubs.push("foo/bar"); })
           sub("foo", function() {
-          pubs.push("foo"); })
+          pubs.push("foo");
+          })
 
           pub("foo")