time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

negate.js (1079B)


      1 /** Error message constants. */
      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 negated 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     var args = arguments;
     30     switch (args.length) {
     31       case 0: return !predicate.call(this);
     32       case 1: return !predicate.call(this, args[0]);
     33       case 2: return !predicate.call(this, args[0], args[1]);
     34       case 3: return !predicate.call(this, args[0], args[1], args[2]);
     35     }
     36     return !predicate.apply(this, args);
     37   };
     38 }
     39 
     40 module.exports = negate;