File tree Expand file tree Collapse file tree 3 files changed +18
-0
lines changed Expand file tree Collapse file tree 3 files changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -82,6 +82,11 @@ export interface IMaybe<T> extends IMonad<T> {
82
82
*/
83
83
map < R > ( f : ( t : T ) => NonNullable < R > ) : IMaybe < R >
84
84
85
+ /**
86
+ * Map to a new value while ignoring previous output value but respecting maybeness
87
+ */
88
+ mapTo < R > ( v : NonNullable < R > ) : IMaybe < R >
89
+
85
90
/**
86
91
* Returns true if value is not empty
87
92
*/
Original file line number Diff line number Diff line change @@ -548,6 +548,13 @@ describe('Maybe', () => {
548
548
} )
549
549
} )
550
550
551
+ describe ( 'mapTo' , ( ) => {
552
+ it ( 'should return new maybe with some' , ( ) => {
553
+ expect ( Maybe . some ( 1 ) . mapTo ( 'deltaforce' ) . valueOrThrowErr ( ) ) . toEqual ( 'deltaforce' )
554
+ expect ( Maybe . none ( ) . mapTo ( 'deltaforce' ) . valueOrNull ( ) ) . toEqual ( null )
555
+ } )
556
+ } )
557
+
551
558
describe ( 'toResult' , ( ) => {
552
559
it ( 'should return result object with success' , ( ) => {
553
560
const hasSome = maybe ( 'hi' )
Original file line number Diff line number Diff line change @@ -86,6 +86,12 @@ export class Maybe<T> implements IMaybe<T> {
86
86
: new Maybe < R > ( )
87
87
}
88
88
89
+ public mapTo < R > ( t : NonNullable < R > ) : IMaybe < R > {
90
+ return this . isSome ( )
91
+ ? new Maybe < R > ( t )
92
+ : new Maybe < R > ( )
93
+ }
94
+
89
95
public flatMap < R > ( fn : ( d : NonNullable < T > ) => IMaybe < R > ) : IMaybe < R > {
90
96
return this . isNone ( ) ? new Maybe < R > ( ) : fn ( this . value as NonNullable < T > )
91
97
}
You can’t perform that action at this time.
0 commit comments