File tree Expand file tree Collapse file tree 3 files changed +22
-9
lines changed Expand file tree Collapse file tree 3 files changed +22
-9
lines changed Original file line number Diff line number Diff line change @@ -138,7 +138,9 @@ export interface IMaybe<T> extends IMonad<T> {
138
138
* Apply a function wrapped in Maybe
139
139
*/
140
140
// eslint-disable-next-line @typescript-eslint/no-explicit-any
141
- apply ( maybe : IMaybe < ReturnType < T extends ( ...args : any ) => any ? T : any > > ) : IMaybe < T >
141
+ // apply(maybe: IMaybe<ReturnType<T extends (...args: any) => any ? T : any>>): IMaybe<T>
142
+
143
+ apply < R > ( fab : IMaybe < ( t : T ) => R > ) : IMaybe < R >
142
144
143
145
toResult < E > ( error : E ) : IResult < T , E >
144
146
}
Original file line number Diff line number Diff line change @@ -573,18 +573,30 @@ describe('Maybe', () => {
573
573
} )
574
574
575
575
describe ( 'apply' , ( ) => {
576
+ it ( 'should return none in nullish cases' , ( ) => {
577
+ const thisNone = maybe < number > ( )
578
+ const fnNone = maybe < ( n : number ) => number > ( )
579
+ const thisSome = maybe ( 5 )
580
+ const fnSome = maybe ( ( a : number ) => a * 2 )
581
+
582
+ expect ( thisNone . apply ( fnNone ) . isNone ( ) ) . toBe ( true )
583
+ expect ( thisNone . apply ( fnSome ) . isNone ( ) ) . toBe ( true )
584
+ expect ( thisSome . apply ( fnNone ) . isNone ( ) ) . toBe ( true )
585
+ } )
586
+
576
587
it ( 'should apply the IMaybe<function>' , ( ) => {
577
- const a = maybe ( ( a : number ) => a * 2 )
578
- const b = maybe ( 5 )
588
+ const a = maybe ( 5 )
589
+ const b = maybe ( ( a : number ) => a * 2 )
579
590
580
591
expect ( a . apply ( b ) . valueOrThrow ( ) ) . toBe ( 10 )
581
592
} )
582
593
583
- it ( 'should apply the non-function maybe ' , ( ) => {
594
+ it ( 'should apply none objects gracefully ' , ( ) => {
584
595
const a = maybe ( 2 )
585
- const b = maybe ( 5 )
596
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
597
+ const b : Maybe < ( a : number ) => number > = maybe ( ( ) => undefined as any )
586
598
587
- expect ( a . apply ( b ) . valueOrThrow ( ) ) . toBe ( 5 )
599
+ expect ( a . apply ( b ) . isNone ( ) ) . toBe ( true )
588
600
} )
589
601
} )
590
602
Original file line number Diff line number Diff line change @@ -129,9 +129,8 @@ export class Maybe<T> implements IMaybe<T> {
129
129
: new Maybe < T > ( )
130
130
}
131
131
132
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
133
- public apply ( maybe : IMaybe < ReturnType < T extends ( ...args : any ) => any ? T : any > > ) : IMaybe < NonNullable < T > > {
134
- return maybe . flatMap ( a => this . map ( b => typeof b === 'function' ? b ( a ) : a ) )
132
+ public apply < R > ( maybeFn : IMaybe < ( t : NonNullable < T > ) => R > ) : IMaybe < NonNullable < R > > {
133
+ return this . flatMap ( v => maybeFn . flatMapAuto ( f => f ( v ) ) )
135
134
}
136
135
137
136
public toResult < E > ( error : E ) : IResult < T , E > {
You can’t perform that action at this time.
0 commit comments