import { ErrorString , InvalidOperationException } from "../../shared" ; param source param predicate throws returns export const single = < TSource > ( source , predicate ? ) : TSource => { if ( predicate ) { return single2 ( source , predicate ) ; } else { return single1 ( source ) ; } } ; const single1 = < TSource > ( source ) : TSource => { let hasValue = false ; let singleValue = null ; for ( const value of source ) { if ( hasValue === true ) { throw new InvalidOperationException ( ErrorString . MoreThanOneElement ) ; } else { hasValue = true ; singleValue = value ; } } if ( hasValue === false ) { throw new InvalidOperationException ( ErrorString . NoElements ) ; } return singleValue as TSource ; } ; const single2 = < TSource > ( source , predicate ) : TSource => { let hasValue = false ; let singleValue = null ; for ( const value of source ) { if ( predicate ( value ) ) { if ( hasValue === true ) { throw new InvalidOperationException ( ErrorString . MoreThanOneMatchingElement ) ; } else { hasValue = true ; singleValue = value ; } } } if ( hasValue === false ) { throw new InvalidOperationException ( ErrorString . NoMatch ) ; } return singleValue as TSource ; } ;