]> source.dussan.org Git - jquery.git/commitdiff
Bug 2616; Adding object support to jQuery.map 252/head
authorJordan Boesch <jboesch26@gmail.com>
Sun, 27 Feb 2011 18:47:35 +0000 (12:47 -0600)
committerJordan Boesch <jboesch26@gmail.com>
Sun, 27 Feb 2011 18:47:35 +0000 (12:47 -0600)
src/core.js
test/unit/core.js

index 036b2db6b9722650938b7187c24b29175cef1090..3ff12085919079d0c3ca7158cca74eb3cebd23bd 100644 (file)
@@ -712,15 +712,29 @@ jQuery.extend({
 
        // arg is for internal usage only
        map: function( elems, callback, arg ) {
-               var ret = [], value;
-
-               // Go through the array, translating each of the items to their
-               // new value (or values).
-               for ( var i = 0, length = elems.length; i < length; i++ ) {
-                       value = callback( elems[ i ], i, arg );
+               var ret = [],
+                       value,
+                       length = elems.length,
+                       // same object detection used in jQuery.each, not full-proof but very speedy.
+                       isObj = length === undefined;
+
+               if ( isObj ) {
+                       for ( key in elems ) {
+                               value = callback( elems[ key ], key, arg );
+
+                               if ( value != null ) {
+                                       ret[ ret.length ] = value;
+                               }
+                       }
+               } else {
+                       // Go through the array, translating each of the items to their
+                       // new value (or values).
+                       for ( var i = 0; i < length; i++ ) {
+                               value = callback( elems[ i ], i, arg );
 
-                       if ( value != null ) {
-                               ret[ ret.length ] = value;
+                               if ( value != null ) {
+                                       ret[ ret.length ] = value;
+                               }
                        }
                }
 
index bce0de0f03ee08b92398524fe9b1b84252f10229..d77e6a9766f5d64cc15b1ea700c4a4669c1c5434 100644 (file)
@@ -608,7 +608,7 @@ test("first()/last()", function() {
 });
 
 test("map()", function() {
-       expect(2);//expect(6);
+       expect(6);
 
        same(
                jQuery("#ap").map(function(){
@@ -625,26 +625,24 @@ test("map()", function() {
                q("ap","ap","ap"),
                "Single Map"
        );
-
-       return;//these haven't been accepted yet
-
+       
        //for #2616
        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" );
 
        var scripts = document.getElementsByTagName("script");
        var mapped = jQuery.map( scripts, function( v, k ){
                return v;
-       }, {length:0} );
+       });
 
        equals( mapped.length, scripts.length, "Map an array(-like) to a hash" );