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

Commit b460577

Browse files
committed
feat: pass non-PII values through methods
1 parent d61d279 commit b460577

File tree

7 files changed

+100
-20
lines changed

7 files changed

+100
-20
lines changed
File renamed without changes.

src/__tests__/fold.spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,12 @@ describe("fold", () => {
88
const folded = fold((sum, n) => sum + n, 0, [one, two, three])
99
expect(unwrap(folded)).toBe(6)
1010
})
11+
12+
it("should fold mixed PII", () => {
13+
const one = PII(1)
14+
const two = 2
15+
const three = PII(3)
16+
const folded = fold((sum, n) => sum + n, 0, [one, two, three])
17+
expect(unwrap(folded)).toBe(6)
18+
})
1119
})

src/__tests__/map.spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,10 @@ describe("map", () => {
66
const mapped = map(a => a.toLowerCase(), result)
77
expect(unwrap(mapped)).toBe("test_string")
88
})
9+
10+
it("should map non PII as well", () => {
11+
const result = "TEST_STRING"
12+
const mapped = map(a => a.toLowerCase(), result)
13+
expect(unwrap(mapped)).toBe("test_string")
14+
})
915
})

src/__tests__/test.spec.ts

+10
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,14 @@ describe("test", () => {
1212
const isIt = test(a => a.includes("sprang"), result)
1313
expect(isIt).toBeFalsy()
1414
})
15+
16+
it("should also test predicate against non-PII", () => {
17+
const resultA = ["TEST_STRING"]
18+
const isItA = test(Array.isArray, resultA)
19+
expect(isItA).toBeTruthy()
20+
21+
const resultB = "TEST_STRING"
22+
const isItB = test(a => a.includes("sprang"), resultB)
23+
expect(isItB).toBeFalsy()
24+
})
1525
})

src/__tests__/unwrap.spec.ts

+4
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ describe("unwrap", () => {
44
it("upwraps a value", () => {
55
expect(unwrap(PII("test"))).toBe("test")
66
})
7+
8+
it("should map non PII as well", () => {
9+
expect(unwrap("test")).toBe("test")
10+
})
711
})

src/__tests__/zipWith.spec.ts

+35
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ describe("zipWith", () => {
88
expect(unwrap(zipped)).toBe("three 3")
99
})
1010

11+
it("should zipWith two mixed types of PII", () => {
12+
const name = PII("three")
13+
const three = 3
14+
const zipped = zip2With((a, b) => `${a} ${b}`, name, three)
15+
expect(unwrap(zipped)).toBe("three 3")
16+
})
17+
1118
it("should zipWith three different types of PII", () => {
1219
const name = PII("three")
1320
const three = PII(3)
@@ -21,6 +28,19 @@ describe("zipWith", () => {
2128
expect(unwrap(zipped)).toBe("three 3 false")
2229
})
2330

31+
it("should zipWith three mixed types of PII", () => {
32+
const name = PII("three")
33+
const three = PII(3)
34+
const nope = false
35+
const zipped = zip3With(
36+
(a, b, c) => `${a} ${b} ${c ? "true" : "false"}`,
37+
name,
38+
three,
39+
nope,
40+
)
41+
expect(unwrap(zipped)).toBe("three 3 false")
42+
})
43+
2444
it("should zipWith four different types of PII", () => {
2545
const name = PII("three")
2646
const three = PII(3)
@@ -35,4 +55,19 @@ describe("zipWith", () => {
3555
)
3656
expect(unwrap(zipped)).toBe("three 3 false six")
3757
})
58+
59+
it("should zipWith four mixed types of PII", () => {
60+
const name = PII("three")
61+
const three = PII(3)
62+
const nope = PII(false)
63+
const who = "six"
64+
const zipped = zip4With(
65+
(a, b, c, d) => `${a} ${b} ${c ? "true" : "false"} ${d}`,
66+
name,
67+
three,
68+
nope,
69+
who,
70+
)
71+
expect(unwrap(zipped)).toBe("three 3 false six")
72+
})
3873
})

src/pii.ts

+37-20
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
12
// Class which wraps PII and keeps logging from accessing it.
23
// eslint-disable-next-line @typescript-eslint/no-unused-vars
34
export interface PII<T> {
@@ -19,48 +20,64 @@ export const PII = <T>(val: T): PII<T> =>
1920
toJSON: () => "PII<REDACTED>",
2021
} as PII<T>)
2122

22-
export const unwrap = <A>(item: PII<A>): A =>
23+
export function unwrap<T>(item: PII<T>): Exclude<T, PII<any>>
24+
export function unwrap<T>(item: T): Exclude<T, PII<any>>
25+
export function unwrap<T>(item: T | PII<T>): Exclude<T, PII<any>> {
2326
// eslint-disable-next-line @typescript-eslint/no-explicit-any
24-
(item as any)[
25-
"__fire_me_if_you_see_me_accessing_this_property_outside_pii_ts"
26-
]
27+
return isPIIType(item)
28+
? (item as any)[
29+
"__fire_me_if_you_see_me_accessing_this_property_outside_pii_ts"
30+
]
31+
: item
32+
}
2733

28-
export const map = <T, T2>(fn: (item: T) => T2, item: PII<T>): PII<T2> =>
29-
PII(fn(unwrap(item)))
34+
export function map<T, T2>(fn: (item: T) => T2, item: PII<T>): PII<T2>
35+
export function map<T, T2>(fn: (item: T) => T2, item: T): Exclude<T2, PII<any>>
36+
export function map<T, T2>(
37+
fn: (item: T) => T2,
38+
item: PII<T> | T,
39+
): PII<T2> | Exclude<T2, PII<any>> {
40+
return PII(fn(unwrap(item)))
41+
}
3042

31-
export const test = <T>(fn: (item: T) => boolean, item: PII<T>): boolean =>
32-
fn(unwrap(item))
43+
export function test<T>(fn: (item: T) => boolean, item: PII<T>): boolean
44+
export function test<T>(fn: (item: T) => boolean, item: T): boolean
45+
export function test<T>(fn: (item: T) => boolean, item: PII<T> | T): boolean {
46+
return fn(unwrap(item))
47+
}
3348

34-
export const fold = <A, B>(
49+
export function fold<A, B>(
3550
fn: (
3651
previousValue: B,
3752
currentValue: A,
3853
currentIndex: number,
3954
array: A[],
4055
) => B,
4156
initial: B,
42-
a: Array<PII<A>>,
43-
): PII<B> => PII(a.map(unwrap).reduce(fn, initial))
57+
a: Array<PII<A> | A>,
58+
): PII<B> {
59+
return PII(a.map<A>(unwrap).reduce(fn, initial))
60+
}
4461

4562
export const zip2With = <A, B, C>(
4663
fn: (a: A, b: B) => C,
47-
a: PII<A>,
48-
b: PII<B>,
64+
a: PII<A> | A,
65+
b: PII<B> | B,
4966
): PII<C> => PII(fn(unwrap(a), unwrap(b)))
5067

5168
export const zip3With = <A, B, C, D>(
5269
fn: (a: A, b: B, c: C) => D,
53-
a: PII<A>,
54-
b: PII<B>,
55-
c: PII<C>,
70+
a: PII<A> | A,
71+
b: PII<B> | B,
72+
c: PII<C> | C,
5673
): PII<D> => PII(fn(unwrap(a), unwrap(b), unwrap(c)))
5774

5875
export const zip4With = <A, B, C, D, E>(
5976
fn: (a: A, b: B, c: C, d: D) => E,
60-
a: PII<A>,
61-
b: PII<B>,
62-
c: PII<C>,
63-
d: PII<D>,
77+
a: PII<A> | A,
78+
b: PII<B> | B,
79+
c: PII<C> | C,
80+
d: PII<D> | D,
6481
): PII<E> => PII(fn(unwrap(a), unwrap(b), unwrap(c), unwrap(d)))
6582

6683
const proto = Object.prototype

0 commit comments

Comments
 (0)