diff --git a/CHANGELOG b/CHANGELOG index 9d1d019..176fbfd 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Extended grep semantics. The first argument to Enumerable#grep is now a "filter" (an object with a match() method) so you can now e.g. filter an array of DOM nodes by CSS selector. RegExp#match is now an alias to RegExp#test, so grep can still be used to filter an array of strings with a regular expression. Closes #7596. [Christophe Porteneuve, sam] + * Make String#scan explicitly return a string. This prevents possible issues with methods expecting input data that is typeof == 'string'. Closes #6350. [AndrewRev, Tobie Langel] * Add Array#intersect for set intersection. Returns a new array containing all items common to the array and the argument, with duplicates removed (clone of the Ruby & method). [Thomas Fuchs] diff --git a/src/base.js b/src/base.js index d99eb1e..36a7fb2 100644 --- a/src/base.js +++ b/src/base.js @@ -140,6 +140,8 @@ var Try = { } } +RegExp.prototype.match = RegExp.prototype.test; + /*--------------------------------------------------------------------------*/ var PeriodicalExecuter = Class.create(); diff --git a/src/enumerable.js b/src/enumerable.js index 16e9b7f..1dba505 100644 --- a/src/enumerable.js +++ b/src/enumerable.js @@ -73,12 +73,15 @@ var Enumerable = { return results; }, - grep: function(pattern, iterator, context) { + grep: function(filter, iterator, context) { iterator = iterator ? iterator.bind(context) : Prototype.K; var results = []; + + if (typeof filter == "string") + filter = new RegExp(filter); + this.each(function(value, index) { - var stringValue = value.toString(); - if (stringValue.match(pattern)) + if (filter.match(value)) results.push(iterator(value, index)); }); return results; diff --git a/test/unit/enumerable.html b/test/unit/enumerable.html index f50fc91..064f247 100644 --- a/test/unit/enumerable.html +++ b/test/unit/enumerable.html @@ -23,6 +23,15 @@
+ + + + + + + +
+ - \ No newline at end of file +