Skip to content

Instantly share code, notes, and snippets.

@vanita5
Created November 26, 2019 11:00
Show Gist options
  • Save vanita5/b34dc854cdde4fd9d9b9087cdf03b75b to your computer and use it in GitHub Desktop.
Save vanita5/b34dc854cdde4fd9d9b9087cdf03b75b to your computer and use it in GitHub Desktop.
Array.prototype.onlySome - Determines whether some but not all members of an array satisfy the specified test.
/**
* Determines whether some but not all members of an array satisfy the specified test.
*
* @param callbackfn A function that accepts up to three arguments. The onlySome method calls
* Array.prototype.some and Array.prototype.every. Both call the callbackfn function for each
* element in the array until the callbackfn returns a value which is coercible to the Boolean value true
* or false respectively, or until the end of the array.
* @param thisArg An object to which the this keyword can refer in the callbackfn function.
* If thisArg is omitted, undefined is used as the this value.
* @returns {boolean}
*/
Array.prototype.onlySome = function(callbackfn, thisArg = undefined) {
if (this === null) {
throw new TypeError("Array.prototype.onlySome called on null or undefined");
}
return !this.every(callbackfn, thisArg) && this.some(callbackfn, thisArg);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment