Add iteratee arity hints to forEach methods. [closes #2277]

This commit is contained in:
John-David Dalton
2016-04-23 11:29:52 -07:00
parent 43c26b5d6f
commit fbc91cf7ae
3 changed files with 44 additions and 20 deletions

View File

@@ -189,25 +189,22 @@ function baseConvert(util, name, func, options) {
if (!isFunction(func)) {
return mixin(func, Object(source));
}
var methods = [],
methodNames = [];
var pairs = [];
each(keys(source), function(key) {
var value = source[key];
if (isFunction(value)) {
methodNames.push(key);
methods.push(func.prototype[key]);
pairs.push([key, func.prototype[key]]);
}
});
mixin(func, Object(source));
each(methodNames, function(methodName, index) {
var method = methods[index];
if (isFunction(method)) {
func.prototype[methodName] = method;
each(pairs, function(pair) {
var value = pair[1];
if (isFunction(value)) {
func.prototype[pair[0]] = value;
} else {
delete func.prototype[methodName];
delete func.prototype[pair[0]];
}
});
return func;

View File

@@ -8500,9 +8500,8 @@
* // => Logs 'a' then 'b' (iteration order is not guaranteed).
*/
function forEach(collection, iteratee) {
return (typeof iteratee == 'function' && isArray(collection))
? arrayEach(collection, iteratee)
: baseEach(collection, getIteratee(iteratee));
var func = isArray(collection) ? arrayEach : baseEach;
return func(collection, getIteratee(iteratee, 3));
}
/**
@@ -8526,9 +8525,8 @@
* // => Logs `2` then `1`.
*/
function forEachRight(collection, iteratee) {
return (typeof iteratee == 'function' && isArray(collection))
? arrayEachRight(collection, iteratee)
: baseEachRight(collection, getIteratee(iteratee));
var func = isArray(collection) ? arrayEachRight : baseEachRight;
return func(collection, getIteratee(iteratee, 3));
}
/**
@@ -12103,7 +12101,7 @@
function forIn(object, iteratee) {
return object == null
? object
: baseFor(object, getIteratee(iteratee), keysIn);
: baseFor(object, getIteratee(iteratee, 3), keysIn);
}
/**
@@ -12135,7 +12133,7 @@
function forInRight(object, iteratee) {
return object == null
? object
: baseForRight(object, getIteratee(iteratee), keysIn);
: baseForRight(object, getIteratee(iteratee, 3), keysIn);
}
/**
@@ -12167,7 +12165,7 @@
* // => Logs 'a' then 'b' (iteration order is not guaranteed).
*/
function forOwn(object, iteratee) {
return object && baseForOwn(object, getIteratee(iteratee));
return object && baseForOwn(object, getIteratee(iteratee, 3));
}
/**
@@ -12197,7 +12195,7 @@
* // => Logs 'b' then 'a' assuming `_.forOwn` logs 'a' then 'b'.
*/
function forOwnRight(object, iteratee) {
return object && baseForOwnRight(object, getIteratee(iteratee));
return object && baseForOwnRight(object, getIteratee(iteratee, 3));
}
/**

View File

@@ -1178,6 +1178,35 @@
});
});
});
/*--------------------------------------------------------------------------*/
QUnit.module('forEach methods');
_.each(['forEach', 'forEachRight', 'forIn', 'forInRight', 'forOwn', 'forOwnRight'], function(methodName) {
var func = fp[methodName];
QUnit.test('`fp.' + methodName + '` should provide `value` to `iteratee`', function(assert) {
assert.expect(2);
var args;
func(function() {
args || (args = slice.call(arguments));
})(['a']);
assert.deepEqual(args, ['a']);
args = undefined;
func(function() {
args || (args = slice.call(arguments));
})({ 'a': 1 });
assert.deepEqual(args, [1]);
});
});
/*--------------------------------------------------------------------------*/
QUnit.module('fp.getOr');