diff --git a/src/core.js b/src/core.js index 01f501d21..6dbc69eb0 100644 --- a/src/core.js +++ b/src/core.js @@ -100,6 +100,18 @@ jQuery.fn = jQuery.prototype = { return this.eq( -1 ); }, + even: function() { + return this.pushStack( jQuery.grep( this, function( _elem, i ) { + return ( i + 1 ) % 2; + } ) ); + }, + + odd: function() { + return this.pushStack( jQuery.grep( this, function( _elem, i ) { + return i % 2; + } ) ); + }, + eq: function( i ) { var len = this.length, j = +i + ( i < 0 ? len : 0 ); diff --git a/test/unit/core.js b/test/unit/core.js index ca644fe20..be09163ac 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -648,6 +648,18 @@ QUnit.test( "first()/last()", function( assert ) { assert.deepEqual( $none.last().get(), [], "last() none" ); } ); +QUnit.test( "even()/odd()", function( assert ) { + assert.expect( 4 ); + + var $links = jQuery( "#ap a" ), $none = jQuery( "asdf" ); + + assert.deepEqual( $links.even().get(), q( "google", "anchor1" ), "even()" ); + assert.deepEqual( $links.odd().get(), q( "groups", "mark" ), "odd()" ); + + assert.deepEqual( $none.even().get(), [], "even() none" ); + assert.deepEqual( $none.odd().get(), [], "odd() none" ); +} ); + QUnit.test( "map()", function( assert ) { assert.expect( 2 );