Skip to content

Commit

Permalink
feat: introduce tapThru function to result. (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickmichalina authored Dec 20, 2022
1 parent c695fcd commit b360d32
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 4 deletions.
18 changes: 18 additions & 0 deletions src/result/result.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ export interface IResult<T, E> {
* Convert Ok result into Fail using a provided value
*/
toFailWhenOkFrom(val: E): IResult<T, E>

/**
* Execute a function with side-effects.
* Returns this to continue operations
*/
tapThru(val: Partial<IResultMatchPattern<T, E, void>>): IResult<T, E>

/**
* Execute a function with side-effects when maybe is a OK.
* Returns this to continue operations
*/
tapOkThru(fn: (val: T) => void): IResult<T, E>

/**
* Execute a function with side-effects when maybe is a Fail.
* Returns this to continue operations
*/
tapFailThru(fn: (val: E) => void): IResult<T, E>
}

export interface IResultOk<T, E = never> extends IResult<T, E> {
Expand Down
125 changes: 121 additions & 4 deletions src/result/result.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ describe('result', () => {
describe('tap', () => {
it('should tap.ok', done => {
const sut = ok(1)

sut.tap({
ok: num => {
expect(num).toEqual(1)
Expand All @@ -215,7 +215,7 @@ describe('result', () => {

it('should tap.ok', done => {
const sut = fail<number, string>('failed')

sut.tap({
fail: str => {
expect(str).toEqual('failed')
Expand All @@ -227,7 +227,7 @@ describe('result', () => {

it('should tapOk', done => {
const sut = ok<number, string>(1)

sut.tapOk(num => {
expect(num).toEqual(1)
done()
Expand All @@ -241,7 +241,7 @@ describe('result', () => {

it('should tapFail', done => {
const sut = fail<number, string>('failed')

sut.tapFail(err => {
expect(err).toEqual('failed')
done()
Expand All @@ -252,5 +252,122 @@ describe('result', () => {
done()
})
})

it('should tapThru', done => {
const result = ok<number, string>(1)

let sideEffect = 0

const sut = result.tapOkThru(v => {
sideEffect = v
}).map(a => a + 2)

expect(sut.unwrap()).toEqual(3)
expect(sideEffect).toEqual(1)

done()
})

it('should tapThru failed side', done => {
const result = fail<number, string>('failed')

let sideEffect = 0

const sut = result.tapOkThru(v => {
sideEffect = v
}).map(a => a + 2)

expect(sut.unwrapFail()).toEqual('failed')
expect(sideEffect).toEqual(0)

done()
})

it('should tapFailThru', done => {
const result = fail<number, string>('failed')

let sideEffect = ''

const sut = result.tapFailThru(v => {
sideEffect = v + ' inside'
}).map(a => a + 2)

expect(sut.unwrapFail()).toEqual('failed')
expect(sideEffect).toEqual('failed inside')

done()
})

it('should tapFailThru Ok side', done => {
const result = ok<number, string>(1)

let sideEffect = ''

const sut = result.tapFailThru(v => {
sideEffect = v + ' inside'
}).map(a => a + 2)

expect(sut.unwrap()).toEqual(3)
expect(sideEffect).toEqual('')

done()
})

it('should tapThru', done => {
const result = ok<number, string>(1)

let sideEffect = 0

const sut = result.tapThru({
ok: v => {
sideEffect = v
}
}).map(a => a + 2)

expect(sut.unwrap()).toEqual(3)
expect(sideEffect).toEqual(1)

done()
})

it('should tapThru', done => {
const result = ok<number, string>(1)

let sideEffect = 0

const sut = result.tapThru({
ok: v => {
sideEffect = v
},
fail: f => {
sideEffect = +f
}
}).map(a => a + 2)

expect(sut.unwrap()).toEqual(3)
expect(sideEffect).toEqual(1)

done()
})

it('should tapThru', done => {
const result = fail<number, string>('failed')

let sideEffect = ''

const sut = result.tapThru({
ok: v => {
sideEffect = v + ''
},
fail: f => {
sideEffect = f + ' in here'
}
}).map(a => a + 2)

expect(sut.unwrapFail()).toEqual('failed')
expect(sideEffect).toEqual('failed in here')

done()
})
})
})
31 changes: 31 additions & 0 deletions src/result/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export abstract class Result<TOk, TFail> implements IResult<TOk, TFail> {
abstract tap(val: IResultMatchPattern<TOk, TFail, void>): void
abstract tapOk(f: (val: TOk) => void): void
abstract tapFail(f: (val: TFail) => void): void
abstract tapThru(val: Partial<IResultMatchPattern<TOk, TFail, void>>): IResult<TOk, TFail>
abstract tapOkThru(fn: (val: TOk) => void): IResult<TOk, TFail>
abstract tapFailThru(fn: (val: TFail) => void): IResult<TOk, TFail>
}

export class OkResult<TOk, TFail> extends Result<TOk, TFail> {
Expand Down Expand Up @@ -94,6 +97,20 @@ export class OkResult<TOk, TFail> extends Result<TOk, TFail> {
}

tapFail(): void { }

tapFailThru(): IResult<TOk, TFail> {
return this
}

tapOkThru(fn: (val: TOk) => void): IResult<TOk, TFail> {
this.tapOk(fn)
return this
}

tapThru(val: Partial<IResultMatchPattern<TOk, TFail, void>>): IResult<TOk, TFail> {
this.tap(val)
return this
}
}

export class FailResult<TOk, TFail> extends Result<TOk, TFail> implements IResult<TOk, TFail> {
Expand Down Expand Up @@ -162,4 +179,18 @@ export class FailResult<TOk, TFail> extends Result<TOk, TFail> implements IResul
tapFail(fn: (val: TFail) => void): void {
fn(this.failureValue)
}

tapFailThru(fn: (val: TFail) => void): IResult<TOk, TFail> {
this.tapFail(fn)
return this
}

tapOkThru(): IResult<TOk, TFail> {
return this
}

tapThru(val: Partial<IResultMatchPattern<TOk, TFail, void>>): IResult<TOk, TFail> {
this.tap(val)
return this
}
}

0 comments on commit b360d32

Please sign in to comment.