From f7a49778ea4d91fc5da2bd9ed0d909c139801c7e Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Tue, 8 Dec 2015 08:02:12 -0800 Subject: [PATCH] Rename `_.modArgs` to `_.overArgs` and remove `_.modArgsSet`. --- lib/fp/mapping.js | 18 +++--- lodash.js | 160 ++++++++++++++++------------------------------ test/test-fp.js | 3 +- test/test.js | 62 +++++++++--------- 4 files changed, 93 insertions(+), 150 deletions(-) diff --git a/lib/fp/mapping.js b/lib/fp/mapping.js index f7e4ad079..57305f3e7 100644 --- a/lib/fp/mapping.js +++ b/lib/fp/mapping.js @@ -19,8 +19,7 @@ module.exports = { 'isEqual': ['equals'], 'mapValues': ['mapObj'], 'matchesProperty': ['pathEq'], - 'modArgs': ['useWith'], - 'modArgsSet': ['converge'], + 'overArgs': ['useWith'], 'omit': ['dissoc', 'omitAll'], 'pick': ['pickAll'], 'property': ['prop'], @@ -82,14 +81,13 @@ module.exports = { 'debounce,defaults,defaultsDeep,delay,difference,drop,dropRight,dropRightWhile,' + 'dropWhile,endsWith,every,extend,filter,find,find,findIndex,findKey,findLast,' + 'findLastIndex,findLastKey,flatMap,forEach,forEachRight,forIn,forInRight,' + - 'forOwn,forOwnRight,get,groupBy,includes,indexBy,indexOf,intersection,' + - 'invoke,invokeMap,isMatch,lastIndexOf,map,mapKeys,mapValues,matchesProperty,' + - 'maxBy,mean,minBy,merge,modArgs,modArgsSet,omit,pad,padLeft,padRight,parseInt,' + - 'partition,pick,pull,pullAll,pullAt,random,range,rangeRight,rearg,reject,' + - 'remove,repeat,result,sampleSize,set,some,sortBy,sortByOrder,sortedIndexBy,' + - 'sortedLastIndexBy,sortedUniqBy,startsWith,subtract,sumBy,take,takeRight,' + - 'takeRightWhile,takeWhile,throttle,times,truncate,union,uniqBy,without,wrap,' + - 'xor,zip,zipObject').split(','), + 'forOwn,forOwnRight,get,groupBy,includes,indexBy,indexOf,intersection,invoke,' + + 'invokeMap,isMatch,lastIndexOf,map,mapKeys,mapValues,matchesProperty,maxBy,' + + 'mean,minBy,merge,omit,overArgs,pad,padLeft,padRight,parseInt,partition,' + + 'pick,pull,pullAll,pullAt,random,range,rangeRight,rearg,reject,remove,repeat,' + + 'result,sampleSize,set,some,sortBy,sortByOrder,sortedIndexBy,sortedLastIndexBy,' + + 'sortedUniqBy,startsWith,subtract,sumBy,take,takeRight,takeRightWhile,takeWhile,' + + 'throttle,times,truncate,union,uniqBy,without,wrap,xor,zip,zipObject').split(','), 3: ( 'assignWith,clamp,differenceBy,extendWith,getOr,inRange,intersectionBy,' + 'isEqualWith,isMatchWith,mergeWith,omitBy,pickBy,pullAllBy,reduce,' + diff --git a/lodash.js b/lodash.js index 4758a51d5..ee69b9b2f 100644 --- a/lodash.js +++ b/lodash.js @@ -1514,13 +1514,13 @@ * `intersection`, `intersectionBy`, `intersectionWith`, invert`, `invokeMap`, * `iteratee`, `keyBy`, `keys`, `keysIn`, `map`, `mapKeys`, `mapValues`, * `matches`, `matchesProperty`, `memoize`, `merge`, `mergeWith`, `method`, - * `methodOf`, `mixin`, `modArgs`, `modArgsSet', `negate`, `nthArg`, `omit`, - * `omitBy`, `once`, `over`, `overEvery`, `overSome`, `partial`, `partialRight`, - * `partition`, `pick`, `pickBy`, `plant`, `property`, `propertyOf`, `pull`, - * `pullAll`, `pullAllBy`, `pullAt`, `push`, `range`, `rangeRight`, `rearg`, - * `reject`, `remove`, `rest`, `reverse`, `sampleSize`, `set`, `setWith`, - * `shuffle`, `slice`, `sort`, `sortBy`, `sortByOrder`, `splice`, `spread`, - * `tail`, `take`, `takeRight`, `takeRightWhile`, `takeWhile`, `tap`, `throttle`, + * `methodOf`, `mixin`, `negate`, `nthArg`, `omit`, `omitBy`, `once`, `over`, + * `overArgs`, `overEvery`, `overSome`, `partial`, `partialRight`, `partition`, + * `pick`, `pickBy`, `plant`, `property`, `propertyOf`, `pull`, `pullAll`, + * `pullAllBy`, `pullAt`, `push`, `range`, `rangeRight`, `rearg`,`reject`, + * `remove`, `rest`, `reverse`, `sampleSize`, `set`, `setWith`, `shuffle`, + * `slice`, `sort`, `sortBy`, `sortByOrder`, `splice`, `spread`, `tail`, + * `take`, `takeRight`, `takeRightWhile`, `takeWhile`, `tap`, `throttle`, * `thru`, `toArray`, `toPairs`, `toPairsIn`, `toPath`, `toPlainObject`, * `transform`, `unary`, `union`, `unionBy`, `unionWith`, `uniq`, `uniqBy`, * `uniqWith`, `unset`, `unshift`, `unzip`, `unzipWith`, `values`, `valuesIn`, @@ -4173,32 +4173,6 @@ return wrapper; } - /** - * Creates a function like `_.modArgs`. - * - * @private - * @param {Function} resolver The function to resolve which invocation - * arguments are provided to each transform. - * @returns {Function} Returns the new arguments modifier function. - */ - function createModArgs(resolver) { - return rest(function(func, transforms) { - transforms = arrayMap(baseFlatten(transforms), getIteratee()); - - var funcsLength = transforms.length; - return rest(function(args) { - var index = -1, - length = nativeMin(args.length, funcsLength), - modded = copyArray(args); - - while (++index < length) { - modded[index] = transforms[index].apply(this, resolver(args[index], index, args)); - } - return func.apply(this, modded); - }); - }); - } - /** * Creates a function like `_.over`. * @@ -8673,76 +8647,6 @@ return memoized; } - /** - * Creates a function that invokes `func` with arguments modified by - * corresponding `transforms`. - * - * @static - * @memberOf _ - * @category Function - * @param {Function} func The function to wrap. - * @param {...(Function|Function[])} [transforms] The functions to transform - * arguments, specified individually or in arrays. - * @returns {Function} Returns the new function. - * @example - * - * function doubled(n) { - * return n * 2; - * } - * - * function square(n) { - * return n * n; - * } - * - * var modded = _.modArgs(function(x, y) { - * return [x, y]; - * }, square, doubled); - * - * modded(9, 3); - * // => [81, 6] - * - * modded(10, 5); - * // => [100, 10] - */ - var modArgs = createModArgs(function(value) { - return [value]; - }); - - /** - * This method is like `_.modArgs` except that each of the `transforms` is - * provided the complete set of arguments the created function is invoked with. - * - * @static - * @memberOf _ - * @category Function - * @param {Function} func The function to wrap. - * @param {...(Function|Function[])} [transforms] The functions to transform - * arguments, specified individually or in arrays. - * @returns {Function} Returns the new function. - * @example - * - * function divide(x, y) { - * return x / y; - * } - * - * function multiply(x, y) { - * return x * y; - * } - * - * var modded = _.modArgsSet(function(x, y) { - * return [x, y]; - * }, multiply, divide); - * - * modded(9, 3); - * // => [27, 3] - * - * modded(10, 5); - * // => [50, 2] - */ - var modArgsSet = createModArgs(function(value, index, args) { - return args; - }); - /** * Creates a function that negates the result of the predicate `func`. The * `func` predicate is invoked with the `this` binding and arguments of the @@ -8792,6 +8696,53 @@ return before(2, func); } + /** + * Creates a function that invokes `func` with arguments transformed by + * corresponding `transforms`. + * + * @static + * @memberOf _ + * @category Function + * @param {Function} func The function to wrap. + * @param {...(Function|Function[])} [transforms] The functions to transform + * arguments, specified individually or in arrays. + * @returns {Function} Returns the new function. + * @example + * + * function doubled(n) { + * return n * 2; + * } + * + * function square(n) { + * return n * n; + * } + * + * var func = _.overArgs(function(x, y) { + * return [x, y]; + * }, square, doubled); + * + * func(9, 3); + * // => [81, 6] + * + * func(10, 5); + * // => [100, 10] + */ + var overArgs = rest(function(func, transforms) { + transforms = arrayMap(baseFlatten(transforms), getIteratee()); + + var funcsLength = transforms.length; + return rest(function(args) { + var index = -1, + length = nativeMin(args.length, funcsLength), + newArgs = copyArray(args); + + while (++index < length) { + newArgs[index] = transforms[index].call(this, args[index]); + } + return apply(func, this, newArgs); + }); + }); + /** * Creates a function that invokes `func` with `partial` arguments prepended * to those provided to the new function. This method is like `_.bind` except @@ -13835,14 +13786,13 @@ lodash.method = method; lodash.methodOf = methodOf; lodash.mixin = mixin; - lodash.modArgs = modArgs; - lodash.modArgsSet = modArgsSet; lodash.negate = negate; lodash.nthArg = nthArg; lodash.omit = omit; lodash.omitBy = omitBy; lodash.once = once; lodash.over = over; + lodash.overArgs = overArgs; lodash.overEvery = overEvery; lodash.overSome = overSome; lodash.partial = partial; diff --git a/test/test-fp.js b/test/test-fp.js index 7f840c639..6ee269cf4 100644 --- a/test/test-fp.js +++ b/test/test-fp.js @@ -120,8 +120,7 @@ var funcMethods = [ 'after', 'ary', 'before', 'bind', 'bindKey', 'cloneDeepWith', 'cloneWith', - 'curryN', 'debounce', 'delay', 'modArgs', 'modArgsSet', 'rearg', 'throttle', - 'wrap' + 'curryN', 'debounce', 'delay', 'overArgs', 'rearg', 'throttle', 'wrap' ]; var exceptions = _.difference(funcMethods.concat('matchesProperty'), ['cloneDeepWith', 'cloneWith', 'delay']), diff --git a/test/test.js b/test/test.js index 963b5dd7c..45f0fecf3 100644 --- a/test/test.js +++ b/test/test.js @@ -13518,75 +13518,72 @@ /*--------------------------------------------------------------------------*/ - QUnit.module('modArgs methods'); - - lodashStable.each(['modArgs', 'modArgsSet'], function(methodName) { - var func = _[methodName], - isModArgs = methodName == 'modArgs'; + QUnit.module('lodash.overArgs'); + (function() { function fn() { return slice.call(arguments); } - QUnit.test('`_.' + methodName + '` should transform each argument', function(assert) { + QUnit.test('should transform each argument', function(assert) { assert.expect(1); - var modded = func(fn, doubled, square); - assert.deepEqual(modded(5, 10), isModArgs ? [10, 100] : [10, 25]); + var over = _.overArgs(fn, doubled, square); + assert.deepEqual(over(5, 10), [10, 100]); }); - QUnit.test('`_.' + methodName + '` should flatten `transforms`', function(assert) { + QUnit.test('should flatten `transforms`', function(assert) { assert.expect(1); - var modded = func(fn, [doubled, square], String); - assert.deepEqual(modded(5, 10, 15), isModArgs ? [10, 100, '15'] : [10, 25, '5']); + var over = _.overArgs(fn, [doubled, square], String); + assert.deepEqual(over(5, 10, 15), [10, 100, '15']); }); - QUnit.test('`_.' + methodName + '` should not transform any argument greater than the number of transforms', function(assert) { + QUnit.test('should not transform any argument greater than the number of transforms', function(assert) { assert.expect(1); - var modded = func(fn, doubled, square); - assert.deepEqual(modded(5, 10, 18), isModArgs ? [10, 100, 18] : [10, 25, 18]); + var over = _.overArgs(fn, doubled, square); + assert.deepEqual(over(5, 10, 18), [10, 100, 18]); }); - QUnit.test('`_.' + methodName + '` should not transform any arguments if no transforms are provided', function(assert) { + QUnit.test('should not transform any arguments if no transforms are provided', function(assert) { assert.expect(1); - var modded = func(fn); - assert.deepEqual(modded(5, 10, 18), [5, 10, 18]); + var over = _.overArgs(fn); + assert.deepEqual(over(5, 10, 18), [5, 10, 18]); }); - QUnit.test('`_.' + methodName + '` should not pass `undefined` if there are more transforms than arguments', function(assert) { + QUnit.test('should not pass `undefined` if there are more transforms than arguments', function(assert) { assert.expect(1); - var modded = func(fn, doubled, identity); - assert.deepEqual(modded(5), [10]); + var over = _.overArgs(fn, doubled, identity); + assert.deepEqual(over(5), [10]); }); - QUnit.test('`_.' + methodName + '` should provide the correct argument to each transform', function(assert) { + QUnit.test('should provide the correct argument to each transform', function(assert) { assert.expect(1); var argsList = [], transform = function() { argsList.push(slice.call(arguments)); }, - modded = func(noop, transform, transform, transform); + over = _.overArgs(noop, transform, transform, transform); - modded('a', 'b'); - assert.deepEqual(argsList, isModArgs ? [['a'], ['b']] : [['a', 'b'], ['a', 'b']]); + over('a', 'b'); + assert.deepEqual(argsList, [['a'], ['b']]); }); - QUnit.test('`_.' + methodName + '` should use `this` binding of function for transforms', function(assert) { + QUnit.test('should use `this` binding of function for transforms', function(assert) { assert.expect(1); - var modded = func(function(x) { + var over = _.overArgs(function(x) { return this[x]; }, function(x) { return this === x; }); - var object = { 'modded': modded, 'true': 1 }; - assert.strictEqual(object.modded(object), 1); + var object = { 'over': over, 'true': 1 }; + assert.strictEqual(object.over(object), 1); }); - }); + }()); /*--------------------------------------------------------------------------*/ @@ -22541,10 +22538,9 @@ var noBinding = [ 'flip', 'memoize', - 'modArgs', - 'modArgsSet', 'negate', 'once', + 'overArgs', 'partial', 'partialRight', 'rearg', @@ -22600,7 +22596,7 @@ var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey); QUnit.test('should accept falsey arguments', function(assert) { - assert.expect(284); + assert.expect(283); var emptyArrays = lodashStable.map(falsey, lodashStable.constant([])); @@ -22688,7 +22684,7 @@ }); QUnit.test('should not set a `this` binding', function(assert) { - assert.expect(33); + assert.expect(30); lodashStable.each(noBinding, function(methodName) { var fn = function() { return this.a; },