From 7be7acfe3072ac1d7a9a55b2fab410c4fa60538a Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sat, 24 Oct 2015 00:37:03 -0700 Subject: [PATCH] Add `_.nthArg`. --- lodash.js | 33 ++++++++++++++++++++++++++++----- test/test.js | 2 +- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/lodash.js b/lodash.js index ed628c848..b7a34aeef 100644 --- a/lodash.js +++ b/lodash.js @@ -1424,11 +1424,11 @@ * `functions`, `groupBy`, `initial`, `intersection`, `intersectionBy`, * `invert`, `invoke`, `iteratee`, `keyBy`, `keys`, `keysIn`, `map`, `mapKeys`, * `mapValues`, `matches`, `matchesProperty`, `memoize`, `merge`, `mergeWith`, - * `method`, `methodOf`, `mixin`, `modArgs`, `modArgsSet', 'negate`, `omit`, - * `omitBy`, `once`, `pairs`, `partial`, `partialRight`, `partition`, `pick`, - * `pickBy`, `plant`, `property`, `propertyOf`, `pull`, `pullAll`, `pullAllBy`, - * `pullAt`, `push`, `range`, `rearg`, `reject`, `remove`, `rest`, `reverse`, - * `sampleSize`, `set`, `setWith`, `shuffle`, `slice`, `sort`, `sortBy`, + * `method`, `methodOf`, `mixin`, `modArgs`, `modArgsSet', 'negate`, `nthArg`, + * `omit`, `omitBy`, `once`, `pairs`, `partial`, `partialRight`, `partition`, + * `pick`, `pickBy`, `plant`, `property`, `propertyOf`, `pull`, `pullAll`, + * `pullAllBy`, `pullAt`, `push`, `range`, `rearg`, `reject`, `remove`, `rest`, + * `reverse`, `sampleSize`, `set`, `setWith`, `shuffle`, `slice`, `sort`, `sortBy`, * `sortByOrder`, `splice`, `spread`, `tail`, `take`, `takeRight`, `takeRightWhile`, * `takeWhile`, `tap`, `throttle`, `thru`, `times`, `toArray`, `toPath`, * `toPlainObject`, `transform`, `union`, `unionBy`, `uniq`, `uniqBy`, `unset`, @@ -12505,6 +12505,28 @@ // No operation performed. } + /** + * Creates a function that returns its nth argument. + * + * @static + * @memberOf _ + * @category Utility + * @param {number} [n=0] The index of the argument to return. + * @returns {Function} Returns the new function. + * @example + * + * var func = _.nthArg(1); + * + * func('a', 'b', 'c'); + * // => 'b' + */ + function nthArg(n) { + n = toInteger(n); + return function() { + return arguments[n]; + }; + } + /** * Creates a function that returns the value at `path` of a given object. * @@ -13050,6 +13072,7 @@ lodash.modArgs = modArgs; lodash.modArgsSet = modArgsSet; lodash.negate = negate; + lodash.nthArg = nthArg; lodash.omit = omit; lodash.omitBy = omitBy; lodash.once = once; diff --git a/test/test.js b/test/test.js index 7faf0552d..fa6d08f76 100644 --- a/test/test.js +++ b/test/test.js @@ -21768,7 +21768,7 @@ var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey); QUnit.test('should accept falsey arguments', function(assert) { - assert.expect(262); + assert.expect(263); var emptyArrays = lodashStable.map(falsey, lodashStable.constant([]));