Skip to content

Commit

Permalink
Allow second argument to be passed to array.indexOf. Fixes #9453.
Browse files Browse the repository at this point in the history
  • Loading branch information
timmywil authored and timmywil committed Sep 19, 2011
1 parent 25205d3 commit 2e0c9bf
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/core.js
Expand Up @@ -682,18 +682,21 @@ jQuery.extend({
return ret; return ret;
}, },


inArray: function( elem, array ) { inArray: function( elem, array, i ) {
if ( !array ) { var len;
return -1;
}


if ( indexOf ) { if ( array ) {
return indexOf.call( array, elem ); if ( indexOf ) {
} return indexOf.call( array, elem, i );
}


for ( var i = 0, length = array.length; i < length; i++ ) { len = array.length;
if ( array[ i ] === elem ) { i = i && i < 0 ? Math.max( 0, len + i ) : 0;
return i;
for ( ; i < len; i++ ) {
if ( array[ i ] === elem ) {
return i;
}
} }
} }


Expand Down
36 changes: 36 additions & 0 deletions test/unit/core.js
Expand Up @@ -627,6 +627,42 @@ test("toArray()", function() {
"Convert jQuery object to an Array" ) "Convert jQuery object to an Array" )
}) })


test("inArray()", function() {
expect(19);

var selections = {
p: q("firstp", "sap", "ap", "first"),
em: q("siblingnext", "siblingfirst"),
div: q("qunit-testrunner-toolbar", "nothiddendiv", "nothiddendivchild", "foo"),
a: q("mark", "groups", "google", "simon1"),
empty: []
},
tests = {
p: { elem: jQuery("#ap")[0], index: 2 },
em: { elem: jQuery("#siblingfirst")[0], index: 1 },
div: { elem: jQuery("#nothiddendiv")[0], index: 1 },
a: { elem: jQuery("#simon1")[0], index: 3 }
},
falseTests = {
p: jQuery("#liveSpan1")[0],
em: jQuery("#nothiddendiv")[0],
empty: ""
};

jQuery.each( tests, function( key, obj ) {
equal( jQuery.inArray( obj.elem, selections[ key ] ), obj.index, "elem is in the array of selections of its tag" );
// Third argument (fromIndex)
equal( !!~jQuery.inArray( obj.elem, selections[ key ], 5 ), false, "elem is NOT in the array of selections given a starting index greater than its position" );
equal( !!~jQuery.inArray( obj.elem, selections[ key ], 1 ), true, "elem is in the array of selections given a starting index less than or equal to its position" );
equal( !!~jQuery.inArray( obj.elem, selections[ key ], -3 ), true, "elem is in the array of selections given a negative index" );
});

jQuery.each( falseTests, function( key, elem ) {
equal( !!~jQuery.inArray( elem, selections[ key ] ), false, "elem is NOT in the array of selections" );
});

});

test("get(Number)", function() { test("get(Number)", function() {
expect(2); expect(2);
equals( jQuery("#qunit-fixture p").get(0), document.getElementById("firstp"), "Get A Single Element" ); equals( jQuery("#qunit-fixture p").get(0), document.getElementById("firstp"), "Get A Single Element" );
Expand Down

0 comments on commit 2e0c9bf

Please sign in to comment.