import { OptionalFindOptions } from './findoptions' ; template T param { AsyncIterable < T > } source param { OptionalFindOptions < T > } options returns { ( T | undefined ) } export function single < T > ( source , options ? ) { const { [ 'thisArg' ] : thisArg , [ 'predicate' ] : predicate = ( ) => true } = options || { } ; let result ; let hasResult = false ; let i = 0 ; for ( const item of source ) { if ( hasResult && predicate ! . call ( thisArg , item , i ++ ) ) { throw new Error ( 'More than one element was found' ) ; } if ( predicate ! . call ( thisArg , item , i ++ ) ) { result = item ; hasResult = true ; } } return result ; }