Rename _.modArgs to _.overArgs and remove _.modArgsSet.

This commit is contained in:
John-David Dalton
2015-12-08 08:02:12 -08:00
parent 58b235b435
commit f7a49778ea
4 changed files with 93 additions and 150 deletions

View File

@@ -19,8 +19,7 @@ module.exports = {
'isEqual': ['equals'], 'isEqual': ['equals'],
'mapValues': ['mapObj'], 'mapValues': ['mapObj'],
'matchesProperty': ['pathEq'], 'matchesProperty': ['pathEq'],
'modArgs': ['useWith'], 'overArgs': ['useWith'],
'modArgsSet': ['converge'],
'omit': ['dissoc', 'omitAll'], 'omit': ['dissoc', 'omitAll'],
'pick': ['pickAll'], 'pick': ['pickAll'],
'property': ['prop'], 'property': ['prop'],
@@ -82,14 +81,13 @@ module.exports = {
'debounce,defaults,defaultsDeep,delay,difference,drop,dropRight,dropRightWhile,' + 'debounce,defaults,defaultsDeep,delay,difference,drop,dropRight,dropRightWhile,' +
'dropWhile,endsWith,every,extend,filter,find,find,findIndex,findKey,findLast,' + 'dropWhile,endsWith,every,extend,filter,find,find,findIndex,findKey,findLast,' +
'findLastIndex,findLastKey,flatMap,forEach,forEachRight,forIn,forInRight,' + 'findLastIndex,findLastKey,flatMap,forEach,forEachRight,forIn,forInRight,' +
'forOwn,forOwnRight,get,groupBy,includes,indexBy,indexOf,intersection,' + 'forOwn,forOwnRight,get,groupBy,includes,indexBy,indexOf,intersection,invoke,' +
'invoke,invokeMap,isMatch,lastIndexOf,map,mapKeys,mapValues,matchesProperty,' + 'invokeMap,isMatch,lastIndexOf,map,mapKeys,mapValues,matchesProperty,maxBy,' +
'maxBy,mean,minBy,merge,modArgs,modArgsSet,omit,pad,padLeft,padRight,parseInt,' + 'mean,minBy,merge,omit,overArgs,pad,padLeft,padRight,parseInt,partition,' +
'partition,pick,pull,pullAll,pullAt,random,range,rangeRight,rearg,reject,' + 'pick,pull,pullAll,pullAt,random,range,rangeRight,rearg,reject,remove,repeat,' +
'remove,repeat,result,sampleSize,set,some,sortBy,sortByOrder,sortedIndexBy,' + 'result,sampleSize,set,some,sortBy,sortByOrder,sortedIndexBy,sortedLastIndexBy,' +
'sortedLastIndexBy,sortedUniqBy,startsWith,subtract,sumBy,take,takeRight,' + 'sortedUniqBy,startsWith,subtract,sumBy,take,takeRight,takeRightWhile,takeWhile,' +
'takeRightWhile,takeWhile,throttle,times,truncate,union,uniqBy,without,wrap,' + 'throttle,times,truncate,union,uniqBy,without,wrap,xor,zip,zipObject').split(','),
'xor,zip,zipObject').split(','),
3: ( 3: (
'assignWith,clamp,differenceBy,extendWith,getOr,inRange,intersectionBy,' + 'assignWith,clamp,differenceBy,extendWith,getOr,inRange,intersectionBy,' +
'isEqualWith,isMatchWith,mergeWith,omitBy,pickBy,pullAllBy,reduce,' + 'isEqualWith,isMatchWith,mergeWith,omitBy,pickBy,pullAllBy,reduce,' +

160
lodash.js
View File

@@ -1514,13 +1514,13 @@
* `intersection`, `intersectionBy`, `intersectionWith`, invert`, `invokeMap`, * `intersection`, `intersectionBy`, `intersectionWith`, invert`, `invokeMap`,
* `iteratee`, `keyBy`, `keys`, `keysIn`, `map`, `mapKeys`, `mapValues`, * `iteratee`, `keyBy`, `keys`, `keysIn`, `map`, `mapKeys`, `mapValues`,
* `matches`, `matchesProperty`, `memoize`, `merge`, `mergeWith`, `method`, * `matches`, `matchesProperty`, `memoize`, `merge`, `mergeWith`, `method`,
* `methodOf`, `mixin`, `modArgs`, `modArgsSet', `negate`, `nthArg`, `omit`, * `methodOf`, `mixin`, `negate`, `nthArg`, `omit`, `omitBy`, `once`, `over`,
* `omitBy`, `once`, `over`, `overEvery`, `overSome`, `partial`, `partialRight`, * `overArgs`, `overEvery`, `overSome`, `partial`, `partialRight`, `partition`,
* `partition`, `pick`, `pickBy`, `plant`, `property`, `propertyOf`, `pull`, * `pick`, `pickBy`, `plant`, `property`, `propertyOf`, `pull`, `pullAll`,
* `pullAll`, `pullAllBy`, `pullAt`, `push`, `range`, `rangeRight`, `rearg`, * `pullAllBy`, `pullAt`, `push`, `range`, `rangeRight`, `rearg`,`reject`,
* `reject`, `remove`, `rest`, `reverse`, `sampleSize`, `set`, `setWith`, * `remove`, `rest`, `reverse`, `sampleSize`, `set`, `setWith`, `shuffle`,
* `shuffle`, `slice`, `sort`, `sortBy`, `sortByOrder`, `splice`, `spread`, * `slice`, `sort`, `sortBy`, `sortByOrder`, `splice`, `spread`, `tail`,
* `tail`, `take`, `takeRight`, `takeRightWhile`, `takeWhile`, `tap`, `throttle`, * `take`, `takeRight`, `takeRightWhile`, `takeWhile`, `tap`, `throttle`,
* `thru`, `toArray`, `toPairs`, `toPairsIn`, `toPath`, `toPlainObject`, * `thru`, `toArray`, `toPairs`, `toPairsIn`, `toPath`, `toPlainObject`,
* `transform`, `unary`, `union`, `unionBy`, `unionWith`, `uniq`, `uniqBy`, * `transform`, `unary`, `union`, `unionBy`, `unionWith`, `uniq`, `uniqBy`,
* `uniqWith`, `unset`, `unshift`, `unzip`, `unzipWith`, `values`, `valuesIn`, * `uniqWith`, `unset`, `unshift`, `unzip`, `unzipWith`, `values`, `valuesIn`,
@@ -4173,32 +4173,6 @@
return wrapper; 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`. * Creates a function like `_.over`.
* *
@@ -8673,76 +8647,6 @@
return memoized; 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 * Creates a function that negates the result of the predicate `func`. The
* `func` predicate is invoked with the `this` binding and arguments of the * `func` predicate is invoked with the `this` binding and arguments of the
@@ -8792,6 +8696,53 @@
return before(2, func); 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 * Creates a function that invokes `func` with `partial` arguments prepended
* to those provided to the new function. This method is like `_.bind` except * to those provided to the new function. This method is like `_.bind` except
@@ -13835,14 +13786,13 @@
lodash.method = method; lodash.method = method;
lodash.methodOf = methodOf; lodash.methodOf = methodOf;
lodash.mixin = mixin; lodash.mixin = mixin;
lodash.modArgs = modArgs;
lodash.modArgsSet = modArgsSet;
lodash.negate = negate; lodash.negate = negate;
lodash.nthArg = nthArg; lodash.nthArg = nthArg;
lodash.omit = omit; lodash.omit = omit;
lodash.omitBy = omitBy; lodash.omitBy = omitBy;
lodash.once = once; lodash.once = once;
lodash.over = over; lodash.over = over;
lodash.overArgs = overArgs;
lodash.overEvery = overEvery; lodash.overEvery = overEvery;
lodash.overSome = overSome; lodash.overSome = overSome;
lodash.partial = partial; lodash.partial = partial;

View File

@@ -120,8 +120,7 @@
var funcMethods = [ var funcMethods = [
'after', 'ary', 'before', 'bind', 'bindKey', 'cloneDeepWith', 'cloneWith', 'after', 'ary', 'before', 'bind', 'bindKey', 'cloneDeepWith', 'cloneWith',
'curryN', 'debounce', 'delay', 'modArgs', 'modArgsSet', 'rearg', 'throttle', 'curryN', 'debounce', 'delay', 'overArgs', 'rearg', 'throttle', 'wrap'
'wrap'
]; ];
var exceptions = _.difference(funcMethods.concat('matchesProperty'), ['cloneDeepWith', 'cloneWith', 'delay']), var exceptions = _.difference(funcMethods.concat('matchesProperty'), ['cloneDeepWith', 'cloneWith', 'delay']),

View File

@@ -13518,75 +13518,72 @@
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
QUnit.module('modArgs methods'); QUnit.module('lodash.overArgs');
lodashStable.each(['modArgs', 'modArgsSet'], function(methodName) {
var func = _[methodName],
isModArgs = methodName == 'modArgs';
(function() {
function fn() { function fn() {
return slice.call(arguments); return slice.call(arguments);
} }
QUnit.test('`_.' + methodName + '` should transform each argument', function(assert) { QUnit.test('should transform each argument', function(assert) {
assert.expect(1); assert.expect(1);
var modded = func(fn, doubled, square); var over = _.overArgs(fn, doubled, square);
assert.deepEqual(modded(5, 10), isModArgs ? [10, 100] : [10, 25]); 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); assert.expect(1);
var modded = func(fn, [doubled, square], String); var over = _.overArgs(fn, [doubled, square], String);
assert.deepEqual(modded(5, 10, 15), isModArgs ? [10, 100, '15'] : [10, 25, '5']); 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); assert.expect(1);
var modded = func(fn, doubled, square); var over = _.overArgs(fn, doubled, square);
assert.deepEqual(modded(5, 10, 18), isModArgs ? [10, 100, 18] : [10, 25, 18]); 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); assert.expect(1);
var modded = func(fn); var over = _.overArgs(fn);
assert.deepEqual(modded(5, 10, 18), [5, 10, 18]); 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); assert.expect(1);
var modded = func(fn, doubled, identity); var over = _.overArgs(fn, doubled, identity);
assert.deepEqual(modded(5), [10]); 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); assert.expect(1);
var argsList = [], var argsList = [],
transform = function() { argsList.push(slice.call(arguments)); }, transform = function() { argsList.push(slice.call(arguments)); },
modded = func(noop, transform, transform, transform); over = _.overArgs(noop, transform, transform, transform);
modded('a', 'b'); over('a', 'b');
assert.deepEqual(argsList, isModArgs ? [['a'], ['b']] : [['a', 'b'], ['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); assert.expect(1);
var modded = func(function(x) { var over = _.overArgs(function(x) {
return this[x]; return this[x];
}, function(x) { }, function(x) {
return this === x; return this === x;
}); });
var object = { 'modded': modded, 'true': 1 }; var object = { 'over': over, 'true': 1 };
assert.strictEqual(object.modded(object), 1); assert.strictEqual(object.over(object), 1);
}); });
}); }());
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
@@ -22541,10 +22538,9 @@
var noBinding = [ var noBinding = [
'flip', 'flip',
'memoize', 'memoize',
'modArgs',
'modArgsSet',
'negate', 'negate',
'once', 'once',
'overArgs',
'partial', 'partial',
'partialRight', 'partialRight',
'rearg', 'rearg',
@@ -22600,7 +22596,7 @@
var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey); var acceptFalsey = lodashStable.difference(allMethods, rejectFalsey);
QUnit.test('should accept falsey arguments', function(assert) { QUnit.test('should accept falsey arguments', function(assert) {
assert.expect(284); assert.expect(283);
var emptyArrays = lodashStable.map(falsey, lodashStable.constant([])); var emptyArrays = lodashStable.map(falsey, lodashStable.constant([]));
@@ -22688,7 +22684,7 @@
}); });
QUnit.test('should not set a `this` binding', function(assert) { QUnit.test('should not set a `this` binding', function(assert) {
assert.expect(33); assert.expect(30);
lodashStable.each(noBinding, function(methodName) { lodashStable.each(noBinding, function(methodName) {
var fn = function() { return this.a; }, var fn = function() { return this.a; },