negate.js 827 B

123456789101112131415161718192021222324252627282930313233
  1. /** Used as the `TypeError` message for "Functions" methods. */
  2. var FUNC_ERROR_TEXT = 'Expected a function';
  3. /**
  4. * Creates a function that negates the result of the predicate `func`. The
  5. * `func` predicate is invoked with the `this` binding and arguments of the
  6. * created function.
  7. *
  8. * @static
  9. * @memberOf _
  10. * @since 3.0.0
  11. * @category Function
  12. * @param {Function} predicate The predicate to negate.
  13. * @returns {Function} Returns the new negated 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. module.exports = negate;