Skip to content

Commit 58d6830

Browse files
feat: introduce tapThru, tapThruSome, tapThruNone (#196)
1 parent bd78b80 commit 58d6830

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed

src/maybe/maybe.interface.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,21 @@ export interface IMaybe<T> extends IMonad<T> {
7373
*/
7474
tapSome(f: (val: T) => void): void
7575

76+
/**
77+
* Execute functions with side-effects.
78+
*/
79+
tapThru(val: Partial<IMaybePattern<T, void>>): IMaybe<T>
80+
81+
/**
82+
* Execute a function with side-effects when maybe is a none.
83+
*/
84+
tapThruNone(f: () => void): IMaybe<T>
85+
86+
/**
87+
* Execute a function with side-effects when maybe is a some.
88+
*/
89+
tapThruSome(f: (val: T) => void): IMaybe<T>
90+
7691
/**
7792
* Unwrap and apply MaybePattern functions
7893
*/

src/maybe/maybe.spec.ts

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { maybe } from './public_api'
1+
import { maybe, none } from './public_api'
22
import { Maybe } from './maybe'
33

44
describe('Maybe', () => {
@@ -616,4 +616,64 @@ describe('Maybe', () => {
616616
expect(sut.unwrapFail()).toEqual(new Error('oops'))
617617
})
618618
})
619+
620+
describe('tapThruSome', () => {
621+
it('should tapThruSome', () => {
622+
// eslint-disable-next-line prefer-const
623+
let variable: undefined | number = undefined
624+
const hasSome = maybe(1)
625+
const sut = hasSome.tapThruSome((v) => {
626+
variable = v + 9
627+
})
628+
expect(sut.isSome()).toBeTruthy()
629+
expect(sut.valueOrThrowErr()).toEqual(1)
630+
expect(variable).toEqual(10)
631+
expect(sut).toBeInstanceOf(Maybe)
632+
})
633+
})
634+
635+
describe('tapThruNone', () => {
636+
it('should tapThruNone', () => {
637+
// eslint-disable-next-line prefer-const
638+
let variable: undefined | string = undefined
639+
const hasSome = none<string>()
640+
const sut = hasSome.tapThruNone(() => {
641+
variable = 'whatever'
642+
})
643+
expect(sut.isNone()).toBeTruthy()
644+
expect(sut.valueOrUndefined()).toBeUndefined()
645+
expect(variable).toEqual('whatever')
646+
expect(sut).toBeInstanceOf(Maybe)
647+
})
648+
})
649+
650+
describe('tapThru', () => {
651+
it('should tap on some ', () => {
652+
// eslint-disable-next-line prefer-const
653+
let variable: undefined | string = undefined
654+
const hasSome = maybe<string>('hi there')
655+
const sut = hasSome.tapThru({
656+
none: () => {},
657+
some: (v) => { variable = v + ' joe'}
658+
})
659+
expect(sut.isSome()).toBeTruthy()
660+
expect(sut.valueOrThrowErr()).toBeTruthy()
661+
expect(variable).toEqual('hi there joe')
662+
expect(sut).toBeInstanceOf(Maybe)
663+
})
664+
665+
it('should tap on none ', () => {
666+
// eslint-disable-next-line prefer-const
667+
let variable: undefined | string = undefined
668+
const hasSome = none<string>()
669+
const sut = hasSome.tapThru({
670+
none: () => { variable = 'sorry joe' },
671+
some: (v) => { variable = v + ' joe' }
672+
})
673+
expect(sut.isNone()).toBeTruthy()
674+
expect(sut.valueOrUndefined()).toBeUndefined()
675+
expect(variable).toEqual('sorry joe')
676+
expect(sut).toBeInstanceOf(Maybe)
677+
})
678+
})
619679
})

src/maybe/maybe.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { IMaybePattern, IMaybe } from './maybe.interface'
55
export class Maybe<T> implements IMaybe<T> {
66

77
constructor(private readonly value?: T | null) { }
8-
8+
99
public of(value: T): IMaybe<T> {
1010
return new Maybe<T>(value)
1111
}
@@ -66,6 +66,21 @@ export class Maybe<T> implements IMaybe<T> {
6666
(this.isSome()) && fn(this.value as NonNullable<T>)
6767
}
6868

69+
public tapThru(val: Partial<IMaybePattern<T, void>>): IMaybe<T> {
70+
this.tap(val)
71+
return this
72+
}
73+
74+
public tapThruNone(fn: () => void): IMaybe<T> {
75+
this.tapNone(fn)
76+
return this
77+
}
78+
79+
public tapThruSome(fn: (val: T) => void): IMaybe<T> {
80+
this.tapSome(fn)
81+
return this
82+
}
83+
6984
public match<R>(pattern: IMaybePattern<T, R>): R {
7085
return this.isNone()
7186
? pattern.none()

0 commit comments

Comments
 (0)