mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-11 19:37:49 +00:00
Add baseFind to simplify _.find, _.findLast, _.findKey, _.findLastKey.
This commit is contained in:
63
lodash.js
63
lodash.js
@@ -1390,6 +1390,30 @@
|
|||||||
return collection;
|
return collection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The base implementation of `find`, 'findLast`, `findKey`, and `findLastKey`
|
||||||
|
* without support for callback shorthands or `thisArg` binding.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
* @param {Array|Object|string} collection The collection to search.
|
||||||
|
* @param {Function} predicate The function called per iteration.
|
||||||
|
* @param {Function} eachFunc The function to iterate over the collection.
|
||||||
|
* @param {boolean} [retKey=false] A flag to indicate returning the key of
|
||||||
|
* the found element instead of the element itself.
|
||||||
|
* @returns {*} Returns the found element or its key, else `undefined`.
|
||||||
|
*/
|
||||||
|
function baseFind(collection, predicate, eachFunc, retKey) {
|
||||||
|
var result;
|
||||||
|
|
||||||
|
eachFunc(collection, function(value, key, collection) {
|
||||||
|
if (predicate(value, key, collection)) {
|
||||||
|
result = retKey ? key : value;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
||||||
@@ -3809,14 +3833,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
var result;
|
return baseFind(collection, predicate, baseEach);
|
||||||
baseEach(collection, function(value, index, collection) {
|
|
||||||
if (predicate(value, index, collection)) {
|
|
||||||
result = value;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3841,16 +3858,8 @@
|
|||||||
* // => 3
|
* // => 3
|
||||||
*/
|
*/
|
||||||
function findLast(collection, predicate, thisArg) {
|
function findLast(collection, predicate, thisArg) {
|
||||||
var result;
|
|
||||||
|
|
||||||
predicate = lodash.createCallback(predicate, thisArg, 3);
|
predicate = lodash.createCallback(predicate, thisArg, 3);
|
||||||
baseEachRight(collection, function(value, index, collection) {
|
return baseFind(collection, predicate, baseEachRight);
|
||||||
if (predicate(value, index, collection)) {
|
|
||||||
result = value;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -5706,16 +5715,8 @@
|
|||||||
* // => 'fred'
|
* // => 'fred'
|
||||||
*/
|
*/
|
||||||
function findKey(object, predicate, thisArg) {
|
function findKey(object, predicate, thisArg) {
|
||||||
var result;
|
|
||||||
|
|
||||||
predicate = lodash.createCallback(predicate, thisArg, 3);
|
predicate = lodash.createCallback(predicate, thisArg, 3);
|
||||||
baseForOwn(object, function(value, key, object) {
|
return baseFind(object, predicate, baseForOwn, true);
|
||||||
if (predicate(value, key, object)) {
|
|
||||||
result = key;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -5760,16 +5761,8 @@
|
|||||||
* // => 'pebbles'
|
* // => 'pebbles'
|
||||||
*/
|
*/
|
||||||
function findLastKey(object, predicate, thisArg) {
|
function findLastKey(object, predicate, thisArg) {
|
||||||
var result;
|
|
||||||
|
|
||||||
predicate = lodash.createCallback(predicate, thisArg, 3);
|
predicate = lodash.createCallback(predicate, thisArg, 3);
|
||||||
baseForOwnRight(object, function(value, key, object) {
|
return baseFind(object, predicate, baseForOwnRight, true);
|
||||||
if (predicate(value, key, object)) {
|
|
||||||
result = key;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user