Reimplement find.

This commit is contained in:
John-David Dalton
2017-03-31 21:36:56 -07:00
parent b679e9cc22
commit 1bdda20e49

13
find.js
View File

@@ -1,5 +1,6 @@
import createFind from './.internal/createFind.js'
import findIndex from './findIndex.js'
import isArrayLike from './isArrayLike'
import keys from './keys'
/**
* Iterates over elements of `collection`, returning the first element
@@ -24,6 +25,14 @@ import findIndex from './findIndex.js'
* find(users, ({ age }) => age < 40)
* // => object for 'barney'
*/
const find = createFind(findIndex)
function find(collection, predicate, fromIndex) {
var iterable = Object(collection);
if (!isArrayLike(collection)) {
collection = keys(collection);
predicate = function(key) { return iteratee(iterable[key], key, iterable); };
}
var index = findIndex(collection, predicate, fromIndex);
return index > -1 ? iterable[iteratee ? collection[index] : index] : undefined;
}
export default find