You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xap-commits@incubator.apache.org by mt...@apache.org on 2006/08/07 17:31:44 UTC

svn commit: r429407 - /incubator/xap/trunk/src/xap/util/ArrayHelper.js

Author: mturyn
Date: Mon Aug  7 10:31:44 2006
New Revision: 429407

URL: http://svn.apache.org/viewvc?rev=429407&view=rev
Log:
Added a unique() method---not in javascript yet--which returns the unique elements of the given array, in the original order of the first instance of each ([a,b,c,a,d]--->[a,b,c,d]).

Modified:
    incubator/xap/trunk/src/xap/util/ArrayHelper.js

Modified: incubator/xap/trunk/src/xap/util/ArrayHelper.js
URL: http://svn.apache.org/viewvc/incubator/xap/trunk/src/xap/util/ArrayHelper.js?rev=429407&r1=429406&r2=429407&view=diff
==============================================================================
--- incubator/xap/trunk/src/xap/util/ArrayHelper.js (original)
+++ incubator/xap/trunk/src/xap/util/ArrayHelper.js Mon Aug  7 10:31:44 2006
@@ -95,3 +95,33 @@
 	}
 	return array ;
 }
+
+
+/**
+ * Returns the elements in an array, unique with respect to "==", and retaining ordering of the
+ * first instances of the non-redundant elements (e.g. [a,b,c,a,d]--> [a.b.c]).  Uses Object's
+ * Set-like properties to prune redundants.<p>
+ * Supposedly, there will be an <array>.unique() in some notional 
+ * future version of the script language....
+ * @param {Array} array The array to which to add.
+ * @return {Array} The altered[?] array, so we can chain this.
+ */ 
+xap.util.ArrayHelper.unique = function( arr ){
+	if (!arr ){
+		return arr;
+	}	
+	
+	var alreadyMoved  = new Object() ;
+
+	var uniques = [] ;
+	for(var ii=0; ii< arr.length ; ++ii){
+		var elem =  arr[ii] ;
+		if( !alreadyMoved[elem]  ){
+			uniques.push( elem ) ;
+			// Don't add it twice:
+			alreadyMoved[elem]  =  true  ;
+		}
+	}
+	return uniques ;
+}
+