nthArg.js 730 B

1234567891011121314151617181920212223242526272829303132
  1. var baseNth = require('./_baseNth'),
  2. baseRest = require('./_baseRest'),
  3. toInteger = require('./toInteger');
  4. /**
  5. * Creates a function that gets the argument at index `n`. If `n` is negative,
  6. * the nth argument from the end is returned.
  7. *
  8. * @static
  9. * @memberOf _
  10. * @since 4.0.0
  11. * @category Util
  12. * @param {number} [n=0] The index of the argument to return.
  13. * @returns {Function} Returns the new pass-thru function.
  14. * @example
  15. *
  16. * var func = _.nthArg(1);
  17. * func('a', 'b', 'c', 'd');
  18. * // => 'b'
  19. *
  20. * var func = _.nthArg(-2);
  21. * func('a', 'b', 'c', 'd');
  22. * // => 'c'
  23. */
  24. function nthArg(n) {
  25. n = toInteger(n);
  26. return baseRest(function(args) {
  27. return baseNth(args, n);
  28. });
  29. }
  30. module.exports = nthArg;