]> source.dussan.org Git - jquery.git/commitdiff
Fix #4262: faster .eq(), closes gh-1000.
authorRichard Gibson <richard.gibson@gmail.com>
Fri, 19 Oct 2012 21:08:50 +0000 (17:08 -0400)
committerDave Methvin <dave.methvin@gmail.com>
Sat, 20 Oct 2012 02:32:25 +0000 (22:32 -0400)
src/core.js
test/unit/core.js

index 2f5908b55dacb1a17342829a807c152f1d4c67b6..907e40eb13f28c12da2592a40ffa3d43845530d3 100644 (file)
@@ -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 ) {
index 2a9ce74e01047acd50b39beb0df1cdbccdfbd93b..4c27f942fb260e0889ec1c36e3be2e457057d214 100644 (file)
@@ -150,20 +150,20 @@ test("jQuery()", function() {
        equal( jQuery(" a<div>" + lng + "</div>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 = "<div />";
-       equal(
-               jQuery(d).appendTo(jQuery(d)).selector,
-               jQuery(d).appendTo(d).selector,
-               "manipulation methods make same selector for jQuery objects"
-       );
 });
 
 test( "globalEval", function() {