Add baseEachRight and baseForOwnRight.

This commit is contained in:
John-David Dalton
2014-01-30 01:36:30 -08:00
parent 876621f8af
commit da1aad7b92

View File

@@ -1386,6 +1386,34 @@
return collection; return collection;
} }
/**
* The base implementation of `_.forEachEach` without support for callback
* shorthands or `thisArg` binding.
*
* @private
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Function} callback The function called per iteration.
* @returns {Array|Object|string} Returns `collection`.
*/
function baseEachRight(collection, callback) {
var iterable = collection,
length = collection ? collection.length : 0;
if (typeof length == 'number') {
if (support.unindexedChars && isString(iterable)) {
iterable = iterable.split('');
}
while (length--) {
if (callback(iterable[length], length, collection) === false) {
break;
}
}
} else {
baseForOwnRight(collection, callback);
}
return collection;
}
/** /**
* The base implementation of `_.flatten` without support for callback * The base implementation of `_.flatten` without support for callback
* shorthands or `thisArg` binding. * shorthands or `thisArg` binding.
@@ -1449,6 +1477,28 @@
return object; return object;
} }
/**
* The base implementation of `_.forOwnRight` without support for callback
* shorthands or `thisArg` binding.
*
* @private
* @param {Object} object The object to iterate over.
* @param {Function} callback The function called per iteration.
* @returns {Object} Returns `object`.
*/
function baseForOwnRight(object, callback) {
var props = keys(object),
length = props.length;
while (length--) {
var key = props[length];
if (callback(object[key], key, object) === false) {
break;
}
}
return object;
}
/** /**
* The base implementation of `_.isEqual`, without support for `thisArg` binding, * The base implementation of `_.isEqual`, without support for `thisArg` binding,
* that allows partial "_.where" style comparisons. * that allows partial "_.where" style comparisons.
@@ -3631,8 +3681,9 @@
*/ */
function findLast(collection, callback, thisArg) { function findLast(collection, callback, thisArg) {
var result; var result;
callback = lodash.createCallback(callback, thisArg, 3); callback = lodash.createCallback(callback, thisArg, 3);
forEachRight(collection, function(value, index, collection) { baseEachRight(collection, function(value, index, collection) {
if (callback(value, index, collection)) { if (callback(value, index, collection)) {
result = value; result = value;
return false; return false;
@@ -3701,27 +3752,15 @@
* // => logs each number from right to left and returns '3,2,1' * // => logs each number from right to left and returns '3,2,1'
*/ */
function forEachRight(collection, callback, thisArg) { function forEachRight(collection, callback, thisArg) {
var iterable = collection, if (callback && typeof thisArg == 'undefined' && isArray(collection)) {
length = collection ? collection.length : 0; var length = collection.length;
callback = callback && typeof thisArg == 'undefined' ? callback : baseCreateCallback(callback, thisArg, 3);
if (isArray(collection)) {
while (length--) { while (length--) {
if (callback(collection[length], length, collection) === false) { if (callback(collection[length], length, collection) === false) {
break; break;
} }
} }
} else { } else {
if (typeof length != 'number') { baseEachRight(collection, baseCreateCallback(callback, thisArg, 3));
var props = keys(collection);
length = props.length;
} else if (support.unindexedChars && isString(collection)) {
iterable = collection.split('');
}
baseEach(iterable, function(value, key) {
key = props ? props[--length] : --length;
return callback(iterable[key], key, collection);
});
} }
return collection; return collection;
} }
@@ -4153,8 +4192,9 @@
*/ */
function reduceRight(collection, callback, accumulator, thisArg) { function reduceRight(collection, callback, accumulator, thisArg) {
var noaccum = arguments.length < 3; var noaccum = arguments.length < 3;
callback = lodash.createCallback(callback, thisArg, 4); callback = lodash.createCallback(callback, thisArg, 4);
forEachRight(collection, function(value, index, collection) { baseEachRight(collection, function(value, index, collection) {
accumulator = noaccum accumulator = noaccum
? (noaccum = false, value) ? (noaccum = false, value)
: callback(accumulator, value, index, collection); : callback(accumulator, value, index, collection);
@@ -4415,12 +4455,20 @@
callback = lodash.createCallback(callback, thisArg, 3); callback = lodash.createCallback(callback, thisArg, 3);
} }
baseEach(collection, function(value, key, collection) { baseEach(collection, function(value, key, collection) {
if (multi) {
var length = callback.length,
criteria = Array(length);
while (length--) {
criteria[length] = value[callback[length]];
}
} else {
criteria = callback(value, key, collection);
}
var object = result[++index] = getObject(); var object = result[++index] = getObject();
object.criteria = criteria;
object.index = index; object.index = index;
object.value = value; object.value = value;
object.criteria = multi
? map(callback, function(key) { return value[key]; })
: callback(value, key, collection);
}); });
length = result.length; length = result.length;
@@ -5497,8 +5545,9 @@
*/ */
function findLastKey(object, callback, thisArg) { function findLastKey(object, callback, thisArg) {
var result; var result;
callback = lodash.createCallback(callback, thisArg, 3); callback = lodash.createCallback(callback, thisArg, 3);
forOwnRight(object, function(value, key, object) { baseForOwnRight(object, function(value, key, object) {
if (callback(value, key, object)) { if (callback(value, key, object)) {
result = key; result = key;
return false; return false;