Skip to main content

Bug Tracker

Side navigation

Ticket #2616: map.diff


File map.diff, 1.3 KB (added by flesler, March 29, 2008 05:27PM UTC)

Proposal

Index: core.js
===================================================================
--- core.js	(revision 5143)
+++ core.js	(working copy)
@@ -1189,23 +1189,27 @@
 		return ret;
 	},
 
-	map: function( elems, callback ) {
-		var ret = [];
+	map: function( source, callback, target ) {
+		if( !target )
+			target = 'length' in source ? [ ] : { };
+		
+		if( 'length' in target ){ //is it an array(-like) ? else a hash
+			var arr = true;
+			if( !target.concat )//fix array-like objects, or they will fail the final concat
+				target = jQuery.makeArray(target);
+		}
 
-		// Go through the array, translating each of the items to their
+		// Go through the items, translating each of them to their
 		// new value (or values).
-		for ( var i = 0, length = elems.length; i < length; i++ ) {
-			var value = callback( elems[ i ], i );
+		jQuery.each( source , function( key, value ){
+			value = callback( value, key );
+			
+			if ( value != null )
+				target[ arr ? target.length : key ] = value;
+		});
 
-			if ( value !== null && value != undefined ) {
-				if ( value.constructor != Array )
-					value = [ value ];
-
-				ret = ret.concat( value );
-			}
-		}
-
-		return ret;
+		//Array.concat can receive arrays and non-arrays too
+		return arr ? target.concat.apply( [], target ) : target;
 	}
 });

Download in other formats:

Original Format