negate.js 803 B

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