From 6c2ada9ffc644d3c26cdc7fa385bf64017025e42 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sun, 1 Nov 2015 15:21:50 -0800 Subject: [PATCH] Expose `_.unary`. --- lodash.js | 47 +++++++++++++++++++++++++++++++++++------------ test/test.js | 5 +++-- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/lodash.js b/lodash.js index 09b9ff204..2d863cfdb 100644 --- a/lodash.js +++ b/lodash.js @@ -847,6 +847,13 @@ return result; } + /** + * The base implementation of `_.unary` without support for storing wrapper metadata. + * + * @private + * @param {Function} func The function to cap arguments for. + * @returns {Function} Returns the new function. + */ function baseUnary(func) { return function(value) { return func(value); @@ -1466,10 +1473,10 @@ * `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`, `unionWith`, - * `uniq`, `uniqBy`, `uniqWith`, `unset`, `unshift`, `unzip`, `unzipWith`, - * `values`, `valuesIn`, `without`, `wrap`, `xor`, `xorBy`, `xorWith`, `zip`, - * `zipObject`, and `zipWith` + * `toPath`, `toPlainObject`, `transform`, `unary`, `union`, `unionBy`, + * `unionWith`, `uniq`, `uniqBy`, `uniqWith`, `unset`, `unshift`, `unzip`, + * `unzipWith`, `values`, `valuesIn`, `without`, `wrap`, `xor`, `xorBy`, + * `xorWith`, `zip`, `zipObject`, and `zipWith` * * The wrapper methods that are **not** chainable by default are: * `add`, `attempt`, `camelCase`, `capitalize`, `ceil`, `clamp`, `clone`, @@ -1488,9 +1495,10 @@ * `reduce`, `reduceRight`, `repeat`, `result`, `round`, `runInContext`, * `sample`, `shift`, `size`, `snakeCase`, `some`, `sortedIndex`, `sortedIndexBy`, * `sortedLastIndex`, `sortedLastIndexBy`, `startCase`, `startsWith`, `sum`, - * `sumBy`, `template`, `toLower`, `toInteger`, `toLength`, `toSafeInteger`, - * `toString`, `toUpper`, `trim`, `trimLeft`, `trimRight`, `truncate`, - * `unescape`, `uniqueId`, `upperCase`, `upperFirst`, `value`, and `words` + * `sumBy`, `template`, `toLower`, `toInteger`, `toLength`, `toNumber`, + * `toSafeInteger`, toString`, `toUpper`, `trim`, `trimLeft`, `trimRight`, + * `truncate`, `unescape`, `uniqueId`, `upperCase`, `upperFirst`, `value`, + * and `words` * * @name _ * @constructor @@ -7779,7 +7787,7 @@ } /** - * Creates a function that accepts up to `n` arguments ignoring any + * Creates a function that accepts up to `n` arguments, ignoring any * additional arguments. * * @static @@ -8720,6 +8728,24 @@ return debounce(func, wait, { 'leading': leading, 'maxWait': +wait, 'trailing': trailing }); } + /** + * Creates a function that accepts up to one argument, ignoring any + * additional arguments. + * + * @static + * @memberOf _ + * @category Function + * @param {Function} func The function to cap arguments for. + * @returns {Function} Returns the new function. + * @example + * + * _.map(['6', '8', '10'], _.unary(parseInt)); + * // => [6, 8, 10] + */ + function unary(func) { + return ary(func, 1); + } + /** * Creates a function that provides `value` to the wrapper function as its * first argument. Any additional arguments provided to the function are @@ -8746,10 +8772,6 @@ return partial(wrapper, value); } - function unary(func) { - return ary(func, 1); - } - /*------------------------------------------------------------------------*/ /** @@ -13411,6 +13433,7 @@ lodash.toPath = toPath; lodash.toPlainObject = toPlainObject; lodash.transform = transform; + lodash.unary = unary; lodash.union = union; lodash.unionBy = unionBy; lodash.unionWith = unionWith; diff --git a/test/test.js b/test/test.js index e770b4f9c..aabf57a40 100644 --- a/test/test.js +++ b/test/test.js @@ -22190,7 +22190,8 @@ 'rearg', 'rest', 'spread', - 'throttle' + 'throttle', + 'unary' ]; var noBinding = [ @@ -22318,7 +22319,7 @@ }); QUnit.test('should throw an error for falsey arguments', function(assert) { - assert.expect(23); + assert.expect(24); lodashStable.each(rejectFalsey, function(methodName) { var expected = lodashStable.map(falsey, lodashStable.constant(true)),