From b5084b4bf29a2e517b90d5e7d82ed17c94a71d94 Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Fri, 19 Oct 2012 17:08:50 -0400 Subject: [PATCH] Fix #4262: faster .eq(), closes gh-1000. --- src/core.js | 15 +++++++-------- test/unit/core.js | 49 +++++++++-------------------------------------- 2 files changed, 16 insertions(+), 48 deletions(-) diff --git a/src/core.js b/src/core.js index 2f5908b55..907e40eb1 100644 --- a/src/core.js +++ b/src/core.js @@ -234,11 +234,9 @@ jQuery.fn = jQuery.prototype = { return this; }, - eq: function( i ) { - i = +i; - return i === -1 ? - this.slice( i ) : - this.slice( i, i + 1 ); + slice: function() { + return this.pushStack( core_slice.apply( this, arguments ), + "slice", core_slice.call(arguments).join(",") ); }, first: function() { @@ -249,9 +247,10 @@ jQuery.fn = jQuery.prototype = { return this.eq( -1 ); }, - slice: function() { - return this.pushStack( core_slice.apply( this, arguments ), - "slice", core_slice.call(arguments).join(",") ); + eq: function( i ) { + var len = this.length, + j = +i + ( i < 0 ? len : 0 ); + return this.pushStack( j >= 0 && j < len ? [ this[j] ] : [] ); }, map: function( callback ) { diff --git a/test/unit/core.js b/test/unit/core.js index 2a9ce74e0..4c27f942f 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -150,20 +150,20 @@ test("jQuery()", function() { equal( jQuery(" a
" + lng + "
b ").length, 1, "Make sure whitespace and other characters are trimmed on long strings." ); }); -test("selector state", function() { - expect(31); +test( "selector state", function() { + expect( 18 ); var test; - test = jQuery(undefined); + test = jQuery( undefined ); equal( test.selector, "", "Empty jQuery Selector" ); equal( test.context, undefined, "Empty jQuery Context" ); - test = jQuery(document); + test = jQuery( document ); equal( test.selector, "", "Document Selector" ); equal( test.context, document, "Document Context" ); - test = jQuery(document.body); + test = jQuery( document.body ); equal( test.selector, "", "Body Selector" ); equal( test.context, document.body, "Body Context" ); @@ -175,53 +175,22 @@ test("selector state", function() { equal( test.selector, "#notfoundnono", "#notfoundnono Selector" ); equal( test.context, document, "#notfoundnono Context" ); - test = jQuery("#qunit-fixture", document); + test = jQuery( "#qunit-fixture", document ); equal( test.selector, "#qunit-fixture", "#qunit-fixture Selector" ); equal( test.context, document, "#qunit-fixture Context" ); - test = jQuery("#qunit-fixture", document.body); + test = jQuery( "#qunit-fixture", document.body ); equal( test.selector, "#qunit-fixture", "#qunit-fixture Selector" ); equal( test.context, document.body, "#qunit-fixture Context" ); // Test cloning - test = jQuery(test); + test = jQuery( test ); equal( test.selector, "#qunit-fixture", "#qunit-fixture Selector" ); equal( test.context, document.body, "#qunit-fixture Context" ); - test = jQuery(document.body).find("#qunit-fixture"); + test = jQuery( document.body ).find("#qunit-fixture"); equal( test.selector, "#qunit-fixture", "#qunit-fixture find Selector" ); equal( test.context, document.body, "#qunit-fixture find Context" ); - - test = jQuery("#qunit-fixture").filter("div"); - equal( test.selector, "#qunit-fixture.filter(div)", "#qunit-fixture filter Selector" ); - equal( test.context, document, "#qunit-fixture filter Context" ); - - test = jQuery("#qunit-fixture").not("div"); - equal( test.selector, "#qunit-fixture.not(div)", "#qunit-fixture not Selector" ); - equal( test.context, document, "#qunit-fixture not Context" ); - - test = jQuery("#qunit-fixture").filter("div").not("div"); - equal( test.selector, "#qunit-fixture.filter(div).not(div)", "#qunit-fixture filter, not Selector" ); - equal( test.context, document, "#qunit-fixture filter, not Context" ); - - test = jQuery("#qunit-fixture").filter("div").not("div").end(); - equal( test.selector, "#qunit-fixture.filter(div)", "#qunit-fixture filter, not, end Selector" ); - equal( test.context, document, "#qunit-fixture filter, not, end Context" ); - - test = jQuery("#qunit-fixture").parent("body"); - equal( test.selector, "#qunit-fixture.parent(body)", "#qunit-fixture parent Selector" ); - equal( test.context, document, "#qunit-fixture parent Context" ); - - test = jQuery("#qunit-fixture").eq(0); - equal( test.selector, "#qunit-fixture.slice(0,1)", "#qunit-fixture eq Selector" ); - equal( test.context, document, "#qunit-fixture eq Context" ); - - var d = "
"; - equal( - jQuery(d).appendTo(jQuery(d)).selector, - jQuery(d).appendTo(d).selector, - "manipulation methods make same selector for jQuery objects" - ); }); test( "globalEval", function() { -- 2.39.5