UNPKG

438 BJavaScriptView Raw
1export function single(source, predicate = () => true) {
2 let result;
3 let hasResult = false;
4 let i = 0;
5 for (const item of source) {
6 if (hasResult && predicate(item, i++)) {
7 throw new Error('More than one element was found');
8 }
9 if (predicate(item, i++)) {
10 result = item;
11 hasResult = true;
12 }
13 }
14 return result;
15}
16
17//# sourceMappingURL=single.mjs.map