Commits

Benoît Bar  committed 1892abf

adding the method 'each' to Array object

  • Participants
  • Parent commits 4ae8d75
  • Branches develop

Comments (0)

Files changed (3)

File src/jscop/core/array.js

 {
     return this[ this.length - 1 ];
 };
+
+/**
+ * Call the callback for all elements of this array
+ * 
+ * @example
+ * 
+ * <pre>
+ * var a = ['a', 'b', 'c'];
+ * a.each(function(index, element){
+ *     alert( index + ' : ' + element);	
+ * });
+ * 
+ * @this {Array}
+ * @param {Function}
+ * 			callback - Required. The callback function for each elements of this array. Can return false to break the loop.
+ * @param {Boolean}
+ * 			compressed - Optional. True if you want to ommit undefined elements in this array.
+ */
+Array.prototype.each = function(callback, compressed)
+{
+	for( var i = 0 ; i < this.length; i++ )
+	{
+		if( compressed && this[i] === undefined )
+		{
+			continue;
+		}
+		else
+		{
+			if( callback(i, this[i]) === false )
+				break;
+		}
+	}
+};

File src/jscop/core/test.js

 };
 
 /**
+ * Throw an exception
+ * 
+ * @param {String}
+ *            caption - Required. The caption of test
+ * @param {mixed}
+ *            message - Required. The exception message
+ */
+jscop.TestCase.prototype.fail = function( caption, message )
+{
+    throw caption+' - '+message;
+};
+
+/**
  * Test event definition
  * 
  * @constant

File test/jscop/core/testArray.js

 .extend(jscop.TestCase);
 
 TestArray.method('testContains', function(){
-    var array = new Array('a', 'b', 'c');
+    var array = ['a', 'b', 'c'];
     this.assertTrue('Testing remove at', array.contains('b'));
     this.assertFalse('Testing remove at', array.contains('v'));
 });
 
 TestArray.method('testRemoveAt', function(){
-    var array = new Array(0, 1, 2);
+    var array = [0, 1, 2];
     array.removeAt(1);
     this.assertTrue('Testing remove at', array.length == 2 && array[0] == 0 && array[1] == 2);
 });
 
 TestArray.method('testRemove', function(){
-    var array = new Array(0, 1, 2);
+    var array = [0, 1, 2];
     array.remove(1);
     this.assertTrue('Testing remove', array.length == 2 && array[0] == 0 && array[1] == 2);
 });
 
 TestArray.method('testRemoveAll', function(){
-    var array = new Array(0, 1, 2, 1);
+    var array = [0, 1, 2, 1];
     array.removeAll(1);
     this.assertTrue('Testing remove all', array.length == 2 && array[0] == 0 && array[1] == 2);
 });
 
 TestArray.method('testLast', function(){
-    var array = new Array('a', 'b', 'c');
+    var array = ['a', 'b', 'c'];
     this.assertTrue('Testing last', array.last() == 'c');
 });
+
+TestArray.method('testEach', function(){
+	var array = ['a', 'b', 'c'];
+	var self = this;
+	array.each(function(index, element){
+		if( index == 0)
+			self.assertTrue('Testing each at index 0', element === 'a');
+		if( index == 1)
+			self.assertTrue('Testing each at index 1', element === 'b');
+		if( index == 2)
+			self.assertTrue('Testing each at index 2', element === 'c');
+	});
+	
+	var elt;
+	array.each(function(index, element){
+		elt = element;
+		if( index == 1 )
+			return false;	
+	});
+	this.assertTrue('Testing each with break', elt === 'b');
+	
+	array = [];
+	array[2] = 'a';
+	array.each(function(index, element){
+		if( index == 0 )
+			self.assertUndefined('Test each compressed', element);
+	});
+	array.each(function(index, element){
+		if( index == 0 || index == 1 )
+			self.fail('Testing each compressed', "This function must not be call because compressed argumet is true");
+		if( index == 2 )
+			self.assertTrue('Test each compressed', element == 'a');
+	}, true);
+});