The language is missing an array method that would test whether or not there are some, but not all elements in the array that pass the test implemented by the provided function.
Currently, one of the more readable ways to do that is to use the logical AND (&&) operator to evaluate the truthy output of the Array.prototype.some()
and the inversed Array.prototype.every()
methods, which isn´t very ergonomic.
With this in mind, I would like to propose a new array method — Array.prototype.part()
. The method tests whether there are some, but not all elements in the array that pass the test implemented by the provided function. The method returns a Boolean value. Here is an example:
([{ name: 'peach', type: 'fruit' }, { name: 'carrot', type: 'vegetable' }]).part(item => item.type === fruit); // => true
([{ name: 'peach', type: 'fruit' }, { name: 'apple', type: 'fruit' }]).part(item => item.type === fruit); // => false
([{ name: 'potato', type: 'vegetable' }, { name: 'carrot', type: 'vegetable' }]).part(item => item.type === fruit); // => false