Skip to content

Commit

Permalink
Core: Implement .even() & .odd() to replace POS :even & :odd
Browse files Browse the repository at this point in the history
`: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 78420d4)
  • Loading branch information
mgol committed Sep 24, 2019
1 parent 0c67da4 commit 409cbda
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
12 changes: 12 additions & 0 deletions test/unit/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down

0 comments on commit 409cbda

Please sign in to comment.