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,
rearg = _.rearg;
var baseAry = function(func, n) {
return function() {
var args = arguments,
length = Math.min(args.length, n);
var baseArity = function(func, n) {
return n == 2
? function(a, b) { return func.apply(undefined, arguments); }
: function(a) { return func.apply(undefined, arguments); };
};
switch (length) {
case 1: return func(args[0]);
case 2: return func(args[0], args[1]);
}
args = Array(length);
while (length--) {
args[length] = arguments[length];
}
return func.apply(undefined, args);
};
var baseAry = function(func, n) {
return n == 2
? function(a, b) { return func(a, b); }
: function(a) { return func(a); };
};
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) {
return function() {
var length = arguments.length,
@@ -172,6 +174,10 @@ function baseConvert(util, name, func) {
if (n) {
result = iterateeAry(result, n);
}
var indexes = mapping.iterateeRearg[name];
if (indexes) {
result = iterateeRearg(result, indexes);
}
if (cap > 1) {
result = curry(result, cap);
}

View File

@@ -102,23 +102,30 @@ module.exports = {
3:[
'assignInWith', 'assignWith', 'clamp', 'differenceBy', 'differenceWith',
'getOr', 'inRange', 'intersectionBy', 'intersectionWith', 'isEqualWith',
'isMatchWith', 'mergeWith', 'pullAllBy', 'reduce',
'reduceRight', 'replace', 'set', 'slice', 'sortedIndexBy', 'sortedLastIndexBy',
'transform', 'unionBy', 'unionWith', 'xorBy', 'xorWith', 'zipWith'
'isMatchWith', 'mergeWith', 'pullAllBy', 'reduce', 'reduceRight', 'replace',
'set', 'slice', 'sortedIndexBy', 'sortedLastIndexBy', 'transform', 'unionBy',
'unionWith', 'xorBy', 'xorWith', 'zipWith'
],
4:[
'fill', 'setWith'
]
},
/** Used to map ary to rearg configs by method ary. */
/** Used to map ary to rearg configs. */
'aryRearg': {
2: [1, 0],
3: [2, 1, 0],
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': {
'clamp': [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');
(function() {
@@ -909,18 +929,16 @@
var args,
value = _.clone(object);
var actual = fp.assignWith(function(objValue, srcValue) {
var actual = fp.assignWith(function() {
args || (args = _.map(arguments, _.cloneDeep));
return srcValue;
}, { 'b': 2 }, value);
assert.deepEqual(args, [undefined, 2, 'b', { 'a': 1 }, { 'b': 2 }], 'fp.assignWith');
args = undefined;
value = _.clone(object);
actual = fp.extendWith(function(objValue, srcValue) {
actual = fp.extendWith(function() {
args || (args = _.map(arguments, _.cloneDeep));
return srcValue;
}, { 'b': 2 }, value);
assert.deepEqual(args, [undefined, 2, 'b', { 'a': 1 }, { 'b': 2 }], 'fp.extendWith');
@@ -930,11 +948,8 @@
args = undefined;
value = { 'a': [1] };
actual = fp.mergeWith(function(objValue, srcValue) {
actual = fp.mergeWith(function() {
args || (args = _.map(arguments, _.cloneDeep));
if (_.isArray(objValue)) {
return objValue.concat(srcValue);
}
}, { 'a': [2, 3] }, value);
args[5] = _.omitBy(args[5], _.isFunction);