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)) { if (!isFunction(func)) {
return mixin(func, Object(source)); return mixin(func, Object(source));
} }
var methods = [], var pairs = [];
methodNames = [];
each(keys(source), function(key) { each(keys(source), function(key) {
var value = source[key]; var value = source[key];
if (isFunction(value)) { if (isFunction(value)) {
methodNames.push(key); pairs.push([key, func.prototype[key]]);
methods.push(func.prototype[key]);
} }
}); });
mixin(func, Object(source)); mixin(func, Object(source));
each(methodNames, function(methodName, index) { each(pairs, function(pair) {
var method = methods[index]; var value = pair[1];
if (isFunction(method)) { if (isFunction(value)) {
func.prototype[methodName] = method; func.prototype[pair[0]] = value;
} else { } else {
delete func.prototype[methodName]; delete func.prototype[pair[0]];
} }
}); });
return func; return func;

View File

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