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 | * @since 3.0.0
|
12 | * @category Function
|
13 | * @param {Function} predicate The predicate to negate.
|
14 | * @returns {Function} Returns the new function.
|
15 | * @example
|
16 | *
|
17 | * function isEven(n) {
|
18 | * return n % 2 == 0;
|
19 | * }
|
20 | *
|
21 | * _.filter([1, 2, 3, 4, 5, 6], _.negate(isEven));
|
22 | * // => [1, 3, 5]
|
23 | */
|
24 | function negate(predicate) {
|
25 | if (typeof predicate != 'function') {
|
26 | throw new TypeError(FUNC_ERROR_TEXT);
|
27 | }
|
28 | return function() {
|
29 | return !predicate.apply(this, arguments);
|
30 | };
|
31 | }
|
32 |
|
33 | export default negate;
|