From 5c0b5d25602ad2f3c8e60a9b57e6f2115650f993 Mon Sep 17 00:00:00 2001 From: Anton M Date: Fri, 19 Nov 2010 12:28:13 +0100 Subject: [PATCH] Improve performance of get() for negative indices. Fixes #5476. --- src/core.js | 2 +- test/unit/core.js | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/core.js b/src/core.js index 9e1bfc6..18cd3a3 100644 --- a/src/core.js +++ b/src/core.js @@ -215,7 +215,7 @@ jQuery.fn = jQuery.prototype = { this.toArray() : // Return just the object - ( num < 0 ? this.slice(num)[ 0 ] : this[ num ] ); + ( num < 0 ? this[ this.length + num ] : this[ num ] ); }, // Take an array of elements and push it onto the stack diff --git a/test/unit/core.js b/test/unit/core.js index 7ef2ad7..7057783 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -547,15 +547,15 @@ test("toArray()", function() { }) test("get(Number)", function() { - expect(1); + expect(2); equals( jQuery("p").get(0), document.getElementById("firstp"), "Get A Single Element" ); + strictEqual( jQuery("#firstp").get(1), undefined, "Try get with index larger elements count" ); }); test("get(-Number)",function() { - expect(1); - equals( jQuery("p").get(-1), - document.getElementById("first"), - "Get a single element with negative index" ) + expect(2); + equals( jQuery("p").get(-1), document.getElementById("first"), "Get a single element with negative index" ); + strictEqual( jQuery("#firstp").get(-2), undefined, "Try get with index negative index larger then elements count" ); }) test("each(Function)", function() { -- 1.7.10.4