From c8a887af066a62840ec1910604ddb7e0c38728e8 Mon Sep 17 00:00:00 2001 From: Jordan Boesch Date: Sun, 27 Feb 2011 12:47:35 -0600 Subject: [PATCH 1/7] Bug 2616; Adding object support to jQuery.map --- src/core.js | 28 +++++++++++++++++++++------- test/unit/core.js | 12 +++++------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/core.js b/src/core.js index 036b2db6..3ff12085 100644 --- a/src/core.js +++ b/src/core.js @@ -712,15 +712,29 @@ jQuery.extend({ // arg is for internal usage only map: function( elems, callback, arg ) { - var ret = [], value; + var ret = [], + value, + length = elems.length, + // same object detection used in jQuery.each, not full-proof but very speedy. + isObj = length === undefined; - // 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 ); + if ( isObj ) { + for ( key in elems ) { + value = callback( elems[ key ], key, arg ); - if ( value != null ) { - ret[ ret.length ] = value; + 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; + } } } diff --git a/test/unit/core.js b/test/unit/core.js index bce0de0f..d77e6a97 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -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" ); From 2407690ef9b3ec59920917935dc1a312f6c2b56e Mon Sep 17 00:00:00 2001 From: Dan Heberden Date: Mon, 21 Mar 2011 08:10:27 -0700 Subject: [PATCH 2/7] Fixes 2616; Pull in #252 by jboesch: jQuery.map with object support --- test/qunit | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/qunit b/test/qunit index d404faf8..cc8460c7 160000 --- a/test/qunit +++ b/test/qunit @@ -1 +1 @@ -Subproject commit d404faf8f587fcbe6b8907943022e6318dd51e0c +Subproject commit cc8460c7b44f023c4f84ab1810b72bf6c6ee4542 From e38f074d14fd65b3f8b0e1bd7956cd75b3dafe2b Mon Sep 17 00:00:00 2001 From: Dan Heberden Date: Mon, 21 Mar 2011 08:39:53 -0700 Subject: [PATCH 3/7] jQuery.map to conform with style guidelines - improved size/DRY code --- src/core.js | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/core.js b/src/core.js index 1077d38c..a0dd7b5b 100644 --- a/src/core.js +++ b/src/core.js @@ -706,29 +706,31 @@ jQuery.extend({ // arg is for internal usage only map: function( elems, callback, arg ) { - var ret = [], - value, + var ret = [], value, i = 0, 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 ); - + + // the work for the loops - run elems[x] through callback + inLoop = function( key ) { + 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; - } + // Go thorugh every key on the object + if ( isObj ) { + 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 ); } } From d832f4f71ec51e67d4cae2557221ef582818f607 Mon Sep 17 00:00:00 2001 From: Dan Heberden Date: Mon, 21 Mar 2011 12:12:31 -0700 Subject: [PATCH 4/7] jQuery.map to iterate over objects with a .length property --- src/core.js | 40 ++++++++++++++++++++-------------------- test/unit/core.js | 12 +++++++----- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/core.js b/src/core.js index a0dd7b5b..951f1b55 100644 --- a/src/core.js +++ b/src/core.js @@ -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; + } } } diff --git a/test/unit/core.js b/test/unit/core.js index c1ffe10b..08d80400 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -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)" ); }); From 00dd6013b6b53455ef7b788801a5dc0616651580 Mon Sep 17 00:00:00 2001 From: Dan Heberden Date: Mon, 21 Mar 2011 12:24:53 -0700 Subject: [PATCH 5/7] Clean up tab spacing --- src/core.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/core.js b/src/core.js index 951f1b55..49c191b9 100644 --- a/src/core.js +++ b/src/core.js @@ -707,10 +707,10 @@ jQuery.extend({ // arg is for internal usage only map: function( elems, callback, 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 ) ); + 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). From c72371f71474cbd6e2318c8ad909a2f92dcb915c Mon Sep 17 00:00:00 2001 From: Dan Heberden Date: Mon, 4 Apr 2011 23:59:54 -0700 Subject: [PATCH 6/7] Improve speed of $.map with object support (-5% previous speed) and improve .length detection --- src/core.js | 15 +++++++-------- src/sizzle | 2 +- test/qunit | 2 +- test/unit/core.js | 4 ++-- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/core.js b/src/core.js index 49c191b9..c9b560ae 100644 --- a/src/core.js +++ b/src/core.js @@ -704,16 +704,15 @@ jQuery.extend({ return ret; }, - // arg is for internal usage only + // arg is for internal usage only map: function( elems, callback, arg ) { var value, ret = [], - i = 0, + i = 0, length = elems.length, - // process .length if it's just an object member - isArray = length !== undefined && ( elems[ length - 1 ] || jQuery.isArray( elems ) ); - + // jquery objects are treated as arrays + isArray = elems instanceof jQuery || length !== undefined && typeof length === "number" && ( ( length > 0 && elems[ 0 ] && 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 ); @@ -723,8 +722,8 @@ jQuery.extend({ } } - // Go thorugh every key on the object, - } else { + // Go thorugh every key on the object, + } else { for ( key in elems ) { value = callback( elems[ key ], key, arg ); diff --git a/src/sizzle b/src/sizzle index ef19279f..f12b9309 160000 --- a/src/sizzle +++ b/src/sizzle @@ -1 +1 @@ -Subproject commit ef19279f54ba49242c6461d47577c703f4f4e80e +Subproject commit f12b9309269ba7e705a99efe099f86ed1fe98d58 diff --git a/test/qunit b/test/qunit index cc8460c7..d404faf8 160000 --- a/test/qunit +++ b/test/qunit @@ -1 +1 @@ -Subproject commit cc8460c7b44f023c4f84ab1810b72bf6c6ee4542 +Subproject commit d404faf8f587fcbe6b8907943022e6318dd51e0c diff --git a/test/unit/core.js b/test/unit/core.js index 08d80400..c15884fc 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -625,7 +625,7 @@ test("map()", function() { q("ap","ap","ap"), "Single Map" ); - + //for #2616 var keys = jQuery.map( {a:1,b:2}, function( v, k ){ return k; @@ -636,7 +636,7 @@ test("map()", function() { 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; From c0389e3e37447aa7eb70cfb4837f4df6cdec5bba Mon Sep 17 00:00:00 2001 From: Dan Heberden Date: Tue, 5 Apr 2011 08:33:14 -0700 Subject: [PATCH 7/7] Add missing var declaration for --- src/core.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core.js b/src/core.js index c9b560ae..f2101099 100644 --- a/src/core.js +++ b/src/core.js @@ -706,7 +706,7 @@ jQuery.extend({ // arg is for internal usage only map: function( elems, callback, arg ) { - var value, ret = [], + var value, key, ret = [], i = 0, length = elems.length, // jquery objects are treated as arrays