mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-29 06:27:49 +00:00
Add iteratee arity hints to forEach methods. [closes #2277]
This commit is contained in:
@@ -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;
|
||||
|
||||
18
lodash.js
18
lodash.js
@@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user