jQuery.map to conform with style guidelines - improved size/DRY code

This commit is contained in:
Dan Heberden 2011-03-21 08:39:53 -07:00
parent 2407690ef9
commit e38f074d14

View file

@ -706,29 +706,31 @@ jQuery.extend({
// arg is for internal usage only // arg is for internal usage only
map: function( elems, callback, arg ) { map: function( elems, callback, arg ) {
var ret = [], var ret = [], value, i = 0,
value,
length = elems.length, length = elems.length,
// same object detection used in jQuery.each, not full-proof but very speedy. // same object detection used in jQuery.each, not full-proof but very speedy.
isObj = length === undefined; isObj = length === undefined;
if ( isObj ) { // the work for the loops - run elems[x] through callback
for ( key in elems ) { inLoop = function( key ) {
value = callback( elems[ key ], key, arg ); value = callback( elems[ key ], key, arg );
if ( value != null ) { if ( value != null ) {
ret[ ret.length ] = value; 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 ) { // Go thorugh every key on the object
ret[ ret.length ] = value; 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 );
} }
} }