I want to propose a function for the Array.prototype similar to Array.includes, which takes a function as an argument: Array.prototype.contains.
Example implementation:
function containsFilter(){
return this.f.call(this.context,...arguments);
}
Object.defineProperty(Array.prototype,'contains',{
...Object.getOwnPropertyDescriptor(Array.prototype,'filter'),
value: function contains(f){
let cf = containsFilter.bind({context:this,f});
return this.filter(cf).length > 0;
}
});
// Why?
function even(n){
return n % 2 === 0;
}
[1,2,3,4,5].contains(even)
true
[1,1,3,5,5].contains(even)
false