Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

before.js 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. var toInteger = require('./toInteger');
  2. /** Error message constants. */
  3. var FUNC_ERROR_TEXT = 'Expected a function';
  4. /**
  5. * Creates a function that invokes `func`, with the `this` binding and arguments
  6. * of the created function, while it's called less than `n` times. Subsequent
  7. * calls to the created function return the result of the last `func` invocation.
  8. *
  9. * @static
  10. * @memberOf _
  11. * @since 3.0.0
  12. * @category Function
  13. * @param {number} n The number of calls at which `func` is no longer invoked.
  14. * @param {Function} func The function to restrict.
  15. * @returns {Function} Returns the new restricted function.
  16. * @example
  17. *
  18. * jQuery(element).on('click', _.before(5, addContactToList));
  19. * // => Allows adding up to 4 contacts to the list.
  20. */
  21. function before(n, func) {
  22. var result;
  23. if (typeof func != 'function') {
  24. throw new TypeError(FUNC_ERROR_TEXT);
  25. }
  26. n = toInteger(n);
  27. return function() {
  28. if (--n > 0) {
  29. result = func.apply(this, arguments);
  30. }
  31. if (n <= 1) {
  32. func = undefined;
  33. }
  34. return result;
  35. };
  36. }
  37. module.exports = before;