@lou.codes/iterables
Version:
🔁 Iterable and AsyncIterable utils
25 lines (24 loc) • 709 B
JavaScript
/**
* Returns the value of the first item in the iterable where predicate is
* `true`, `undefined` otherwise.
*
* @category Reducers
* @example
* ```typescript
* const findEven = find((number: number) => number % 2 === 0);
* findEven([1, 2, 3, 4]); // 2
* findEven([1, 3, 5, 7]); // undefined
* ```
* @param predicate Predicate function to search for item.
* @returns Curried function with `predicate` set in context.
*/
export const find = predicate => iterable => {
// eslint-disable-next-line functional/no-loop-statements
for (const item of iterable) {
// eslint-disable-next-line functional/no-conditional-statements
if (predicate(item)) {
return item;
}
}
return undefined;
};