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> {
33
33
*/
34
34
valueOrUndefined ( ) : T | undefined
35
35
36
+ /**
37
+ * Unwrap a Maybe with its value or return and empty list
38
+ */
39
+ toArray ( ) : ReadonlyArray < T >
40
+
36
41
/**
37
42
* Unwrap a Maybe with a default computed value
38
43
*/
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ const isEmpty = <T>(value: T) => value === null || value === undefined
4
4
const isNotEmpty = < T > ( value : T ) => ! isEmpty ( value )
5
5
const valueOr = < T > ( value ?: T ) => ( val : NonNullable < T > ) => isEmpty ( value ) ? val : value as NonNullable < T >
6
6
const 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 > ]
7
8
const valueOrCompute = < T > ( value ?: T ) => ( fn : ( ) => NonNullable < T > ) => isEmpty ( value ) ? fn ( ) : value as NonNullable < T >
8
9
const tap = < T > ( value ?: T ) => ( obj : Partial < IMaybePattern < T , void > > ) => isEmpty ( value ) ? obj . none && obj . none ( ) : obj . some && obj . some ( value as NonNullable < T > )
9
10
const tapNone = < T > ( value ?: T ) => ( fn : ( ) => void ) => ( isEmpty ( value ) ) && fn ( )
@@ -18,6 +19,7 @@ export const maybe = <T>(value?: T): IMaybe<NonNullable<T>> => {
18
19
valueOr : valueOr ( value ) ,
19
20
valueOrUndefined : valueOrUndefined ( value ) ,
20
21
valueOrCompute : valueOrCompute ( value ) ,
22
+ toArray : toArray ( value ) ,
21
23
tap : tap ( value ) ,
22
24
tapNone : tapNone ( value ) ,
23
25
tapSome : tapSome ( value ) ,
Original file line number Diff line number Diff line change @@ -347,4 +347,30 @@ describe('Maybe', () => {
347
347
expect ( maybeAString ) . toEqual ( 'actual input' )
348
348
} )
349
349
} )
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
+ } )
350
376
} )
You can’t perform that action at this time.
0 commit comments