File tree Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -547,4 +547,20 @@ describe('Maybe', () => {
547
547
expect ( Maybe . some ( 1 ) . valueOrThrowErr ( ) ) . toEqual ( 1 )
548
548
} )
549
549
} )
550
+
551
+ describe ( 'toResult' , ( ) => {
552
+ it ( 'should return result object with success' , ( ) => {
553
+ const hasSome = maybe ( 'hi' )
554
+ const sut = hasSome . toResult ( new Error ( 'oops' ) )
555
+
556
+ expect ( sut . unwrap ( ) ) . toEqual ( 'hi' )
557
+ } )
558
+
559
+ it ( 'should return result object with fail' , ( ) => {
560
+ const hasSome = maybe ( )
561
+ const sut = hasSome . toResult ( new Error ( 'oops' ) )
562
+
563
+ expect ( sut . unwrapFail ( ) ) . toEqual ( new Error ( 'oops' ) )
564
+ } )
565
+ } )
550
566
} )
Original file line number Diff line number Diff line change
1
+ import { IResult } from '../result/result.interface'
2
+ import { FailResult , OkResult } from '../result/result'
1
3
import { IMaybePattern , IMaybe } from './maybe.interface'
2
4
3
5
export class Maybe < T > implements IMaybe < T > {
@@ -107,4 +109,10 @@ export class Maybe<T> implements IMaybe<T> {
107
109
return maybe . flatMap ( a => this . map ( b => typeof b === 'function' ? b ( a ) : a ) )
108
110
}
109
111
112
+ public toResult < E > ( error : E ) : IResult < T , E > {
113
+ return this
114
+ . map < IResult < T , E > > ( b => new OkResult < T , E > ( b ) )
115
+ . valueOr ( new FailResult < T , E > ( error ) )
116
+ }
117
+
110
118
}
You can’t perform that action at this time.
0 commit comments