File tree Expand file tree Collapse file tree 3 files changed +33
-0
lines changed Expand file tree Collapse file tree 3 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -33,6 +33,11 @@ export interface IMaybe<T> extends IMonad<T> {
3333 */
3434 valueOrUndefined ( ) : T | undefined
3535
36+ /**
37+ * Unwrap a Maybe with its value or return and empty list
38+ */
39+ toArray ( ) : ReadonlyArray < T >
40+
3641 /**
3742 * Unwrap a Maybe with a default computed value
3843 */
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ const isEmpty = <T>(value: T) => value === null || value === undefined
44const isNotEmpty = < T > ( value : T ) => ! isEmpty ( value )
55const valueOr = < T > ( value ?: T ) => ( val : NonNullable < T > ) => isEmpty ( value ) ? val : value as NonNullable < T >
66const valueOrUndefined = < T > ( value ?: T ) => ( ) => isEmpty ( value ) ? undefined : value as NonNullable < T >
7+ const toArray = < T > ( value ?: T ) => ( ) => isEmpty ( value ) ? [ ] : Array . isArray ( value ) ? value : [ value as NonNullable < T > ]
78const valueOrCompute = < T > ( value ?: T ) => ( fn : ( ) => NonNullable < T > ) => isEmpty ( value ) ? fn ( ) : value as NonNullable < T >
89const tap = < T > ( value ?: T ) => ( obj : Partial < IMaybePattern < T , void > > ) => isEmpty ( value ) ? obj . none && obj . none ( ) : obj . some && obj . some ( value as NonNullable < T > )
910const tapNone = < T > ( value ?: T ) => ( fn : ( ) => void ) => ( isEmpty ( value ) ) && fn ( )
@@ -18,6 +19,7 @@ export const maybe = <T>(value?: T): IMaybe<NonNullable<T>> => {
1819 valueOr : valueOr ( value ) ,
1920 valueOrUndefined : valueOrUndefined ( value ) ,
2021 valueOrCompute : valueOrCompute ( value ) ,
22+ toArray : toArray ( value ) ,
2123 tap : tap ( value ) ,
2224 tapNone : tapNone ( value ) ,
2325 tapSome : tapSome ( value ) ,
Original file line number Diff line number Diff line change @@ -347,4 +347,30 @@ describe('Maybe', () => {
347347 expect ( maybeAString ) . toEqual ( 'actual input' )
348348 } )
349349 } )
350+
351+ describe ( 'when returning an array' , ( ) => {
352+ it ( 'should handle "none" case' , ( ) => {
353+ const sut = undefined as string | undefined
354+ const maybeThing = maybe ( sut ) . toArray ( )
355+
356+ expect ( maybeThing ) . toHaveLength ( 0 )
357+ expect ( maybeThing ) . toEqual ( [ ] )
358+ } )
359+
360+ it ( 'should handle "some" case' , ( ) => {
361+ const sut = 'actual input' as string | undefined
362+ const maybeThing = maybe ( sut ) . toArray ( )
363+
364+ expect ( maybeThing ) . toHaveLength ( 1 )
365+ expect ( maybeThing ) . toEqual ( [ 'actual input' ] )
366+ } )
367+
368+ it ( 'should handle "some" case existing array' , ( ) => {
369+ const sut = [ 'actual input' ] as ReadonlyArray < string > | undefined
370+ const maybeThing = maybe ( sut ) . toArray ( )
371+
372+ expect ( maybeThing ) . toHaveLength ( 1 )
373+ expect ( maybeThing ) . toEqual ( [ 'actual input' ] )
374+ } )
375+ } )
350376} )
You can’t perform that action at this time.
0 commit comments