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

Commit c2bcb39

Browse files
committed
feat: add tap
1 parent 7d7b431 commit c2bcb39

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/__tests__/tap.spec.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { PII, tap, unwrap } from "../index"
2+
3+
describe("tap", () => {
4+
it("should tap inside PII", () => {
5+
const result = PII("TEST_STRING")
6+
let b: string | undefined = undefined
7+
const mapped = tap(a => (b = a), result)
8+
expect(unwrap(mapped)).toBe(b)
9+
})
10+
11+
it("should tap non PII as well", () => {
12+
const result = "TEST_STRING"
13+
let b: string | undefined = undefined
14+
const mapped = tap(a => (b = a), result)
15+
expect(unwrap(mapped)).toBe(b)
16+
})
17+
})

src/pii.ts

+8
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ export function map<T, T2>(
4040
return PII(fn(unwrap(item)))
4141
}
4242

43+
export function tap<T>(fn: (item: T) => void, item: PII<T>): PII<T>
44+
export function tap<T>(fn: (item: T) => void, item: T): T
45+
export function tap<T>(fn: (item: T) => void, item: PII<T> | T): PII<T> | T {
46+
fn(unwrap(item))
47+
48+
return item
49+
}
50+
4351
export function test<T>(fn: (item: T) => boolean, item: PII<T>): boolean
4452
export function test<T>(fn: (item: T) => boolean, item: T): boolean
4553
export function test<T>(fn: (item: T) => boolean, item: PII<T> | T): boolean {

0 commit comments

Comments
 (0)