Ensure fp key methods provide the key argument to their iteratees. [closes #1842]

This commit is contained in:
John-David Dalton
2016-01-21 23:15:52 -06:00
parent 68d0238044
commit b13b17e801
3 changed files with 55 additions and 27 deletions

View File

@@ -40,21 +40,16 @@ function baseConvert(util, name, func) {
keys = _.keys, keys = _.keys,
rearg = _.rearg; rearg = _.rearg;
var baseAry = function(func, n) { var baseArity = function(func, n) {
return function() { return n == 2
var args = arguments, ? function(a, b) { return func.apply(undefined, arguments); }
length = Math.min(args.length, n); : function(a) { return func.apply(undefined, arguments); };
};
switch (length) { var baseAry = function(func, n) {
case 1: return func(args[0]); return n == 2
case 2: return func(args[0], args[1]); ? function(a, b) { return func(a, b); }
} : function(a) { return func(a); };
args = Array(length);
while (length--) {
args[length] = arguments[length];
}
return func.apply(undefined, args);
};
}; };
var cloneArray = function(array) { var cloneArray = function(array) {
@@ -83,6 +78,13 @@ function baseConvert(util, name, func) {
}); });
}; };
var iterateeRearg = function(func, indexes) {
return overArg(func, function(func) {
var n = indexes.length;
return baseArity(rearg(baseAry(func, n), indexes), n);
});
};
var overArg = function(func, iteratee, retArg) { var overArg = function(func, iteratee, retArg) {
return function() { return function() {
var length = arguments.length, var length = arguments.length,
@@ -172,6 +174,10 @@ function baseConvert(util, name, func) {
if (n) { if (n) {
result = iterateeAry(result, n); result = iterateeAry(result, n);
} }
var indexes = mapping.iterateeRearg[name];
if (indexes) {
result = iterateeRearg(result, indexes);
}
if (cap > 1) { if (cap > 1) {
result = curry(result, cap); result = curry(result, cap);
} }

View File

@@ -102,23 +102,30 @@ module.exports = {
3:[ 3:[
'assignInWith', 'assignWith', 'clamp', 'differenceBy', 'differenceWith', 'assignInWith', 'assignWith', 'clamp', 'differenceBy', 'differenceWith',
'getOr', 'inRange', 'intersectionBy', 'intersectionWith', 'isEqualWith', 'getOr', 'inRange', 'intersectionBy', 'intersectionWith', 'isEqualWith',
'isMatchWith', 'mergeWith', 'pullAllBy', 'reduce', 'isMatchWith', 'mergeWith', 'pullAllBy', 'reduce', 'reduceRight', 'replace',
'reduceRight', 'replace', 'set', 'slice', 'sortedIndexBy', 'sortedLastIndexBy', 'set', 'slice', 'sortedIndexBy', 'sortedLastIndexBy', 'transform', 'unionBy',
'transform', 'unionBy', 'unionWith', 'xorBy', 'xorWith', 'zipWith' 'unionWith', 'xorBy', 'xorWith', 'zipWith'
], ],
4:[ 4:[
'fill', 'setWith' 'fill', 'setWith'
] ]
}, },
/** Used to map ary to rearg configs by method ary. */ /** Used to map ary to rearg configs. */
'aryRearg': { 'aryRearg': {
2: [1, 0], 2: [1, 0],
3: [2, 1, 0], 3: [2, 1, 0],
4: [3, 2, 0, 1] 4: [3, 2, 0, 1]
}, },
/** Used to map ary to rearg configs by method names. */ /** Used to map method names to iteratee rearg configs. */
'iterateeRearg': {
'findKey': [1],
'findLastKey': [1],
'mapKeys': [1]
},
/** Used to map method names to rearg configs. */
'methodRearg': { 'methodRearg': {
'clamp': [2, 0, 1], 'clamp': [2, 0, 1],
'reduce': [2, 0, 1], 'reduce': [2, 0, 1],

View File

@@ -773,6 +773,26 @@
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
QUnit.module('key methods');
(function() {
var object = { 'a': 1 };
QUnit.test('should provide the correct `iteratee` arguments', function(assert) {
assert.expect(1);
var args;
var actual = fp.findKey(function() {
args || (args = _.map(arguments, _.cloneDeep));
}, object);
assert.deepEqual(args, ['a'], 'fp.findKey');
});
}());
/*--------------------------------------------------------------------------*/
QUnit.module('mutation methods'); QUnit.module('mutation methods');
(function() { (function() {
@@ -909,18 +929,16 @@
var args, var args,
value = _.clone(object); value = _.clone(object);
var actual = fp.assignWith(function(objValue, srcValue) { var actual = fp.assignWith(function() {
args || (args = _.map(arguments, _.cloneDeep)); args || (args = _.map(arguments, _.cloneDeep));
return srcValue;
}, { 'b': 2 }, value); }, { 'b': 2 }, value);
assert.deepEqual(args, [undefined, 2, 'b', { 'a': 1 }, { 'b': 2 }], 'fp.assignWith'); assert.deepEqual(args, [undefined, 2, 'b', { 'a': 1 }, { 'b': 2 }], 'fp.assignWith');
args = undefined; args = undefined;
value = _.clone(object); value = _.clone(object);
actual = fp.extendWith(function(objValue, srcValue) { actual = fp.extendWith(function() {
args || (args = _.map(arguments, _.cloneDeep)); args || (args = _.map(arguments, _.cloneDeep));
return srcValue;
}, { 'b': 2 }, value); }, { 'b': 2 }, value);
assert.deepEqual(args, [undefined, 2, 'b', { 'a': 1 }, { 'b': 2 }], 'fp.extendWith'); assert.deepEqual(args, [undefined, 2, 'b', { 'a': 1 }, { 'b': 2 }], 'fp.extendWith');
@@ -930,11 +948,8 @@
args = undefined; args = undefined;
value = { 'a': [1] }; value = { 'a': [1] };
actual = fp.mergeWith(function(objValue, srcValue) { actual = fp.mergeWith(function() {
args || (args = _.map(arguments, _.cloneDeep)); args || (args = _.map(arguments, _.cloneDeep));
if (_.isArray(objValue)) {
return objValue.concat(srcValue);
}
}, { 'a': [2, 3] }, value); }, { 'a': [2, 3] }, value);
args[5] = _.omitBy(args[5], _.isFunction); args[5] = _.omitBy(args[5], _.isFunction);