import Optional from 'optional-js' ; param supplier return export function safeGet < T > ( supplier ) { let value ; try { value = supplier ( ) ! ; } catch ( e ) { return Optional . empty ( ) ; } return Optional . ofNullable ( value ) ! ; } export async function safeGetAsync < T > ( supplier ) { let value ; try { value = await supplier ( ) ; } catch ( e ) { return Optional . empty ( ) ; } return Optional . ofNullable ( value ) ; } export function mapIfPresentOrElse < T > ( optional , presentAction , notPresentAction ) { if ( optional . isPresent ( ) ) { return presentAction ( optional . get ( ) ) ; } else { return notPresentAction ( ) ; } } export function mapIfPresent < T > ( optional , presentAction ) { if ( optional . isPresent ( ) ) { return presentAction ( optional . get ( ) ) ; } } export function trimToEmpty ( value ) { if ( value === undefined ) { return Optional . empty ( ) ; } const trimmed = value . trim ( ) ; if ( trimmed === '' ) { return Optional . empty ( ) ; } return Optional . of ( trimmed ) ; } export const ofNullable = Optional . ofNullable ; export default { mapIfPresent , mapIfPresentOrElse , safeGet , safeGetAsync , trimToEmpty } ;