Fix _.forEachRight, _.forInRight, and _.findLastIndex and add related unit tests.

Former-commit-id: 5131ae4559cd71d8016745f85158bb6f96426d01
This commit is contained in:
John-David Dalton
2013-08-04 12:46:27 -07:00
parent 1a529841e0
commit 0f6de542bf
2 changed files with 53 additions and 40 deletions

View File

@@ -2102,17 +2102,16 @@
* // => logs 'name' and 'bark' assuming `_.forIn ` logs 'bark' and 'name'
*/
function forInRight(object, callback, thisArg) {
var index = -1,
pairs = [];
var pairs = [];
forIn(object, function(value, key) {
pairs.push(value, key);
pairs.push(key, value);
});
var length = pairs.length;
callback = baseCreateCallback(callback, thisArg, 3);
while (++index < length) {
if (callback(pairs[index], pairs[++index], object) === false) {
while (length--) {
if (callback(pairs[length--], pairs[length], object) === false) {
break;
}
}
@@ -3317,7 +3316,7 @@
callback = baseCreateCallback(callback, thisArg, 3);
forEach(collection, function(value, index, collection) {
index = props ? props[--length] : --length;
callback(iterable[index], index, collection);
return callback(iterable[index], index, collection);
});
return collection;
}
@@ -4196,13 +4195,11 @@
* // => 2
*/
function findLastIndex(array, callback, thisArg) {
var index = -1,
length = array ? array.length : 0;
var length = array ? array.length : 0;
callback = lodash.createCallback(callback, thisArg);
while (length--) {
if (callback(array[index], index, array)) {
return index;
if (callback(array[length], length, array)) {
return length;
}
}
return -1;