]> source.dussan.org Git - jquery.git/commitdiff
jQuery.map to iterate over objects with a .length property
authorDan Heberden <danheberden@gmail.com>
Mon, 21 Mar 2011 19:12:31 +0000 (12:12 -0700)
committerDan Heberden <danheberden@gmail.com>
Mon, 21 Mar 2011 19:12:31 +0000 (12:12 -0700)
src/core.js
test/unit/core.js

index a0dd7b5b3b7946a3bb6e20365eb62005bc0cf358..951f1b559abd82e703e950e179bece3d755eeacb 100644 (file)
@@ -704,33 +704,33 @@ jQuery.extend({
                return ret;
        },
 
-       // arg is for internal usage only
+               // arg is for internal usage only
        map: function( elems, callback, arg ) {
-               var ret = [], value, i = 0,
-                       length = elems.length,
-                       // same object detection used in jQuery.each, not full-proof but very speedy.
-                       isObj = length === undefined;
-                       
-                       // the work for the loops - run elems[x] through callback
-                       inLoop = function( key ) {
-                         value = callback( elems[ key ], key, arg );
-                         
+               var value, ret = [],
+                                       i = 0,          
+                                       length = elems.length,
+                                       // process .length if it's just an object member
+                                       isArray = length !== undefined && ( elems[ length - 1 ] || jQuery.isArray( elems ) );
+               
+               // Go through the array, translating each of the items to their
+               // new value (or values).
+               if ( isArray ) {
+                       for ( ; i < length; i++ ) {
+                               value = callback( elems[ i ], i, arg );
+
                                if ( value != null ) {
                                        ret[ ret.length ] = value;
                                }
                        }
 
-    // Go thorugh every key on the object
-               if ( isObj ) {
+               // Go thorugh every key on the object, 
+               } else {                
                        for ( key in elems ) {
-                         inLoop( key );
-                       }
-               
-               // Go through the array, translating each of the items to their
-               // new value (or values).
-               } else {                        
-                       for ( ; i < length; i++ ) {
-                               inLoop( i );
+                               value = callback( elems[ key ], key, arg );
+
+                               if ( value != null ) {
+                                       ret[ ret.length ] = value;
+                               }
                        }
                }
 
index c1ffe10b455ef8ce55da09a266f95b686457354c..08d80400c7cdefb88261c8ef6df0c44f60475f0e 100644 (file)
@@ -608,7 +608,7 @@ test("first()/last()", function() {
 });
 
 test("map()", function() {
-       expect(6);
+       expect(7);
 
        same(
                jQuery("#ap").map(function(){
@@ -630,26 +630,28 @@ test("map()", function() {
        var keys = jQuery.map( {a:1,b:2}, function( v, k ){
                return k;
        });
-
        equals( keys.join(""), "ab", "Map the keys from a hash to an array" );
 
        var values = jQuery.map( {a:1,b:2}, function( v, k ){
                return v;
        });
-
        equals( values.join(""), "12", "Map the values from a hash to an array" );
+       
+       // object with length prop
+       var values = jQuery.map( {a:1,b:2, length:3}, function( v, k ){
+               return v;
+       });
+       equals( values.join(""), "123", "Map the values from a hash with a length property to an array" );
 
        var scripts = document.getElementsByTagName("script");
        var mapped = jQuery.map( scripts, function( v, k ){
                return v;
        });
-
        equals( mapped.length, scripts.length, "Map an array(-like) to a hash" );
 
        var flat = jQuery.map( Array(4), function( v, k ){
                return k % 2 ? k : [k,k,k];//try mixing array and regular returns
        });
-
        equals( flat.join(""), "00012223", "try the new flatten technique(#2616)" );
 });