import { MonoTypeOperatorFunction , Observable , ObservableInput , of , OperatorFunction } from 'rxjs' ; import { filter , map , mergeMap , switchMap , tap } from 'rxjs/operators' ; import { Err , Ok , Result } from '../index' ; export function resultMap < T , T2 , E > ( mapper ) { return ( source ) => { return source . pipe ( map ( ( result ) => result . map ( mapper ) ) ) ; } ; } export function resultMapErr < T , E , E2 > ( mapper ) { return ( source ) => { return source . pipe ( map ( ( result ) => result . mapErr ( mapper ) ) ) ; } ; } export function resultMapTo < T , T2 , E > ( value ) { return ( source ) => { return source . pipe ( map ( ( result ) => result . map ( ( ) => value ) ) ) ; } ; } export function resultMapErrTo < T , E , E2 > ( value ) { return ( source ) => { return source . pipe ( map ( ( result ) => result . mapErr ( ( ) => value ) ) ) ; } ; } export function elseMap < T , E , E2 > ( mapper ) { return ( source ) => { return source . pipe ( map ( ( result ) => { if ( result . err ) { return mapper ( result . val ) ; } else { return result . val ; } } ) ) ; } ; } export function elseMapTo < T , E , E2 > ( value ) { return ( source ) => { return source . pipe ( map ( ( result ) => { if ( result . err ) { return value ; } else { return result . val ; } } ) ) ; } ; } export function resultSwitchMap < T , E , T2 , E2 > ( mapper ) ; export function resultSwitchMap < T , T2 , E > ( mapper ) ; export function resultSwitchMap < T , E , T2 , E2 > ( mapper ) { return ( source ) => { return source . pipe ( switchMap ( ( result ) => { if ( result . ok ) { return mapper ( result . val ) ; } else { return of ( result ) ; } } ) , map ( ( result ) => { if ( Result . isResult ( result ) ) { return result ; } else { return new Ok ( result ) ; } } ) ) ; } ; } export function resultMergeMap < T , E , T2 , E2 > ( mapper ) ; export function resultMergeMap < T , T2 , E > ( mapper ) ; export function resultMergeMap < T , E , T2 , E2 > ( mapper ) { return ( source ) => { return source . pipe ( mergeMap ( ( result ) => { if ( result . ok ) { return mapper ( result . val ) ; } else { return of ( result ) ; } } ) , map ( ( result ) => { if ( Result . isResult ( result ) ) { return result ; } else { return new Ok ( result ) ; } } ) ) ; } ; } export function filterResultOk < T , E > ( ) { return ( source ) => { return source . pipe ( filter ( ( result ) : result is Ok < T > => result . ok ) , map ( ( result ) => result . val ) ) ; } ; } export function filterResultErr < T , E > ( ) { return ( source ) => { return source . pipe ( filter ( ( result ) : result is Err < E > => result . err ) , map ( ( result ) => result . val ) ) ; } ; } export function tapResultErr < T , E > ( tapFn ) { return ( source ) => { return source . pipe ( tap ( ( r ) => { if ( ! r . ok ) { tapFn ( r . val ) ; } } ) ) ; } ; } export function tapResultOk < T , E > ( tapFn ) { return ( source ) => { return source . pipe ( tap ( ( r ) => { if ( r . ok ) { tapFn ( r . val ) ; } } ) ) ; } ; }