ary.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. var createWrapper = require('../internal/createWrapper'),
  2. isIterateeCall = require('../internal/isIterateeCall');
  3. /** Used to compose bitmasks for wrapper metadata. */
  4. var ARY_FLAG = 128;
  5. /* Native method references for those with the same name as other `lodash` methods. */
  6. var nativeMax = Math.max;
  7. /**
  8. * Creates a function that accepts up to `n` arguments ignoring any
  9. * additional arguments.
  10. *
  11. * @static
  12. * @memberOf _
  13. * @category Function
  14. * @param {Function} func The function to cap arguments for.
  15. * @param {number} [n=func.length] The arity cap.
  16. * @param- {Object} [guard] Enables use as a callback for functions like `_.map`.
  17. * @returns {Function} Returns the new function.
  18. * @example
  19. *
  20. * _.map(['6', '8', '10'], _.ary(parseInt, 1));
  21. * // => [6, 8, 10]
  22. */
  23. function ary(func, n, guard) {
  24. if (guard && isIterateeCall(func, n, guard)) {
  25. n = undefined;
  26. }
  27. n = (func && n == null) ? func.length : nativeMax(+n || 0, 0);
  28. return createWrapper(func, ARY_FLAG, undefined, undefined, undefined, undefined, n);
  29. }
  30. module.exports = ary;