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).

This commit is contained in:
Thomas Fuchs 2007-07-18 21:07:31 +00:00
parent cea24d6cc2
commit 513042dd98
3 changed files with 22 additions and 0 deletions

View File

@ -1,5 +1,9 @@
*SVN*
* 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]
Example:
[1,1,3,5].intersect([1,2,3]) -> [1,3]
* Rename Element#getElementsBySelector to Element#select and add alias for Element#getElementsBySelector. [Thomas Fuchs]
* Add Element#adjacent as a shortcut to selecting all adjacent nodes (and their children) that match a CSS selector. [Thomas Fuchs]

View File

@ -85,6 +85,12 @@ Object.extend(Array.prototype, {
});
},
intersect: function(array) {
return this.uniq().findAll(function(item) {
return array.include(item);
});
},
clone: function() {
return [].concat(this);
},

View File

@ -147,6 +147,18 @@
assertEqual('[\'a\', 1]',['a',1].inspect());
}},
testIntersect: function(){ with(this) {
assertEnumEqual([1,3], [1,1,3,5].intersect([1,2,3]));
assertEnumEqual([1], [1,1].intersect([1,1]));
assertEnumEqual([], [1,1,3,5].intersect([4]));
assertEnumEqual([], [1].intersect(['1']));
assertEnumEqual(
['B','C','D'],
$R('A','Z').toArray().intersect($R('B','D').toArray())
);
}},
testToJSON: function(){ with(this) {
assertEqual('[]', [].toJSON());
assertEqual('[\"a\"]', ['a'].toJSON());