prototype: 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. Closes #7596.
This commit is contained in:
parent
38fa39af92
commit
b59399bdd3
@ -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]
|
||||
|
@ -140,6 +140,8 @@ var Try = {
|
||||
}
|
||||
}
|
||||
|
||||
RegExp.prototype.match = RegExp.prototype.test;
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
var PeriodicalExecuter = Class.create();
|
||||
|
@ -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;
|
||||
|
@ -23,6 +23,15 @@
|
||||
<!-- Log output -->
|
||||
<div id="testlog"> </div>
|
||||
|
||||
<table id="grepTable">
|
||||
<tbody id="grepTBody">
|
||||
<tr id="grepRow">
|
||||
<th id="grepHeader" class="cell"></th>
|
||||
<td id="grepCell" class="cell"></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<!-- Tests follow -->
|
||||
<script type="text/javascript" language="javascript" charset="utf-8">
|
||||
// <![CDATA[
|
||||
@ -182,6 +191,9 @@
|
||||
Fixtures.Nicknames.grep(/o/, function(nickname) {
|
||||
return nickname.toUpperCase();
|
||||
}).join(", "))
|
||||
|
||||
assertEnumEqual($('grepHeader', 'grepCell'),
|
||||
$('grepTable', 'grepTBody', 'grepRow', 'grepHeader', 'grepCell').grep(new Selector('.cell')));
|
||||
}},
|
||||
|
||||
testInclude: function() {with(this) {
|
||||
@ -309,4 +321,4 @@
|
||||
// ]]>
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
||||
|
Loading…
Reference in New Issue
Block a user