Skip to content

Commit 6335c8d

Browse files
feat(maybe): introduce toArray function (#42)
1 parent 8fcba51 commit 6335c8d

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

src/interfaces/maybe.interface.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff 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
*/

src/monads/maybe.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const isEmpty = <T>(value: T) => value === null || value === undefined
44
const isNotEmpty = <T>(value: T) => !isEmpty(value)
55
const valueOr = <T>(value?: T) => (val: NonNullable<T>) => isEmpty(value) ? val : value as NonNullable<T>
66
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>]
78
const valueOrCompute = <T>(value?: T) => (fn: () => NonNullable<T>) => isEmpty(value) ? fn() : value as NonNullable<T>
89
const tap = <T>(value?: T) => (obj: Partial<IMaybePattern<T, void>>) => isEmpty(value) ? obj.none && obj.none() : obj.some && obj.some(value as NonNullable<T>)
910
const 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),

test/monads/maybe.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff 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
})

0 commit comments

Comments
 (0)