import { ErrorString , InvalidOperationException } from "../../shared" ; param source param predicate throws returns export const singleOrDefault = < TSource > ( source , predicate ? ) : TSource | null => { if ( predicate ) { return singleOrDefault2 ( source , predicate ) ; } else { return singleOrDefault1 ( source ) ; } } ; const singleOrDefault1 = < TSource > ( source ) : TSource | null => { let hasValue = false ; let singleValue = null ; for ( const value of source ) { if ( hasValue === true ) { throw new InvalidOperationException ( ErrorString . MoreThanOneElement ) ; } else { hasValue = true ; singleValue = value ; } } return singleValue ; } ; const singleOrDefault2 = < TSource > ( source , predicate ) : TSource | null => { 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 ; } } } return singleValue ; } ;