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> {
138138 * Apply a function wrapped in Maybe
139139 */
140140 // 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 >
142144
143145 toResult < E > ( error : E ) : IResult < T , E >
144146}
Original file line number Diff line number Diff line change @@ -573,18 +573,30 @@ describe('Maybe', () => {
573573 } )
574574
575575 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+
576587 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 )
579590
580591 expect ( a . apply ( b ) . valueOrThrow ( ) ) . toBe ( 10 )
581592 } )
582593
583- it ( 'should apply the non-function maybe ' , ( ) => {
594+ it ( 'should apply none objects gracefully ' , ( ) => {
584595 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 )
586598
587- expect ( a . apply ( b ) . valueOrThrow ( ) ) . toBe ( 5 )
599+ expect ( a . apply ( b ) . isNone ( ) ) . toBe ( true )
588600 } )
589601 } )
590602
Original file line number Diff line number Diff line change @@ -129,9 +129,8 @@ export class Maybe<T> implements IMaybe<T> {
129129 : new Maybe < T > ( )
130130 }
131131
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 ) ) )
135134 }
136135
137136 public toResult < E > ( error : E ) : IResult < T , E > {
You can’t perform that action at this time.
0 commit comments