From: Michał Gołębiowski-Owczarek Date: Tue, 24 Sep 2019 00:04:53 +0000 (+0200) Subject: Core: Implement .even() & .odd() to replace POS :even & :odd X-Git-Tag: 3.5.0~45 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=409cbda7fe85d7026aaa4423d3384f288eed5be2;p=jquery.git Core: Implement .even() & .odd() to replace POS :even & :odd `:even` & `:odd` are deprecated since jQuery 3.4.0 & will be removed in 4.0.0. The new `even()` & `odd()` methods will make the migration easier. Closes gh-4485 (cherry picked from commit 78420d427cf3734d9264405fcbe08b76be182a95) --- diff --git a/src/core.js b/src/core.js index 3ef92d48f..dde341cc8 100644 --- a/src/core.js +++ b/src/core.js @@ -105,6 +105,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 2f40462f3..b8e556b85 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -670,6 +670,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 );