Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80742 views
1
/** Used as the `TypeError` message for "Functions" methods. */
2
var FUNC_ERROR_TEXT = 'Expected a function';
3
4
/**
5
* Creates a function that negates the result of the predicate `func`. The
6
* `func` predicate is invoked with the `this` binding and arguments of the
7
* created function.
8
*
9
* @static
10
* @memberOf _
11
* @category Function
12
* @param {Function} predicate The predicate to negate.
13
* @returns {Function} Returns the new function.
14
* @example
15
*
16
* function isEven(n) {
17
* return n % 2 == 0;
18
* }
19
*
20
* _.filter([1, 2, 3, 4, 5, 6], _.negate(isEven));
21
* // => [1, 3, 5]
22
*/
23
function negate(predicate) {
24
if (typeof predicate != 'function') {
25
throw new TypeError(FUNC_ERROR_TEXT);
26
}
27
return function() {
28
return !predicate.apply(this, arguments);
29
};
30
}
31
32
module.exports = negate;
33
34