Skip to content
This repository has been archived by the owner on Apr 2, 2023. It is now read-only.

Commit

Permalink
feat: add tap
Browse files Browse the repository at this point in the history
  • Loading branch information
tdreyno committed Jun 2, 2021
1 parent 7d7b431 commit c2bcb39
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/__tests__/tap.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { PII, tap, unwrap } from "../index"

describe("tap", () => {
it("should tap inside PII", () => {
const result = PII("TEST_STRING")
let b: string | undefined = undefined
const mapped = tap(a => (b = a), result)
expect(unwrap(mapped)).toBe(b)
})

it("should tap non PII as well", () => {
const result = "TEST_STRING"
let b: string | undefined = undefined
const mapped = tap(a => (b = a), result)
expect(unwrap(mapped)).toBe(b)
})
})
8 changes: 8 additions & 0 deletions src/pii.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ export function map<T, T2>(
return PII(fn(unwrap(item)))
}

export function tap<T>(fn: (item: T) => void, item: PII<T>): PII<T>
export function tap<T>(fn: (item: T) => void, item: T): T
export function tap<T>(fn: (item: T) => void, item: PII<T> | T): PII<T> | T {
fn(unwrap(item))

return item
}

export function test<T>(fn: (item: T) => boolean, item: PII<T>): boolean
export function test<T>(fn: (item: T) => boolean, item: T): boolean
export function test<T>(fn: (item: T) => boolean, item: PII<T> | T): boolean {
Expand Down

0 comments on commit c2bcb39

Please sign in to comment.