From 079d651e10bc19a28c49945617e842415dc41981 Mon Sep 17 00:00:00 2001 From: John Resig Date: Fri, 31 Aug 2007 03:26:03 +0000 Subject: [PATCH] Added $(...).map() functionality. (Also closes #1250, imo) --- src/jquery/coreTest.js | 20 ++++++++++++++++++++ src/jquery/jquery.js | 6 ++++++ 2 files changed, 26 insertions(+) diff --git a/src/jquery/coreTest.js b/src/jquery/coreTest.js index e91928497..40be7f870 100644 --- a/src/jquery/coreTest.js +++ b/src/jquery/coreTest.js @@ -976,3 +976,23 @@ test("slice()", function() { isSet( $("#ap a").slice(0,3), q("google", "groups", "anchor1"), "slice(0,3)" ); isSet( $("#ap a").slice(-1), q("mark"), "slice(-1)" ); }); + +test("map()", function() { + expect(2); + + isSet( + $("#ap").map(function(){ + return $(this).find("a").get(); + }), + q("google", "groups", "anchor1", "mark"), + "Array Map" + ); + + isSet( + $("#ap > a").map(function(){ + return this.parentNode; + }), + q("ap","ap","ap"), + "Single Map" + ); +}); diff --git a/src/jquery/jquery.js b/src/jquery/jquery.js index 8988c5ee1..8180a4ea2 100644 --- a/src/jquery/jquery.js +++ b/src/jquery/jquery.js @@ -1189,6 +1189,12 @@ jQuery.fn = jQuery.prototype = { slice: function() { return this.pushStack( Array.prototype.slice.apply( this, arguments ) ); }, + + map: function(fn){ + return this.pushStack(jQuery.map( this, function(elem,i){ + return fn.call( elem, i, elem ); + })); + }, /** * @private -- 2.39.5