Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
28 lines (27 sloc)
688 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Creates a function that negates the result of the predicate `func`. The | |
* `func` predicate is invoked with the `this` binding and arguments of the | |
* created function. | |
* | |
* @since 3.0.0 | |
* @category Function | |
* @param {Function} predicate The predicate to negate. | |
* @returns {Function} Returns the new negated function. | |
* @example | |
* | |
* function isEven(n) { | |
* return n % 2 == 0 | |
* } | |
* | |
* filter([1, 2, 3, 4, 5, 6], negate(isEven)) | |
* // => [1, 3, 5] | |
*/ | |
function negate(predicate) { | |
if (typeof predicate !== 'function') { | |
throw new TypeError('Expected a function') | |
} | |
return function(...args) { | |
return !predicate.apply(this, args) | |
} | |
} | |
export default negate |