Skip to content

Commit

Permalink
fix(collections): enable to work short circuit for undefined values
Browse files Browse the repository at this point in the history
  • Loading branch information
NotFounds committed Sep 1, 2021
1 parent 95dc34e commit 8864057
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion collections/single.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ export function single<T>(
predicate: (el: T) => boolean = (_) => true,
): T | undefined {
let match: T | undefined = undefined;
let found = false;
for (const element of array) {
if (predicate(element)) {
if (match !== undefined) {
if (found) {
return undefined;
}
found = true;
match = element;
}
}
Expand Down

0 comments on commit 8864057

Please sign in to comment.