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

Commit d8437a4

Browse files
committed
feat: add optional maxDepth option to recursive functions: containsPII, detect, redact, unwrapObject
1 parent 9dd6f37 commit d8437a4

32 files changed

+329
-221
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ module.exports = {
2424
"@typescript-eslint/no-unsafe-return": 0,
2525
"@typescript-eslint/no-unsafe-call": 0,
2626
"@typescript-eslint/no-unsafe-member-access": 0,
27+
"@typescript-eslint/no-explicit-any": 0,
2728
},
2829
}

src/PIITry.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import PII from "./pii"
2+
3+
export default async function PIITry<S>(fn: () => S | Promise<S>): Promise<S> {
4+
try {
5+
return fn()
6+
} catch (e) {
7+
throw PII(e, "REDACTED_ERROR")
8+
}
9+
}

src/__tests__/constructor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { PII, unwrap } from "../index"
1+
import PII from "../pii"
2+
import unwrap from "../unwrap"
23

34
describe("PII", () => {
45
it("should not leak into toString", () => {

src/__tests__/containsPII.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { PII, containsPII } from "../index"
1+
import PII from "../pii"
2+
import containsPII from "../containsPII"
23

34
describe("containsPII", () => {
45
it("should not find PII", () => {
@@ -20,4 +21,8 @@ describe("containsPII", () => {
2021
expect(containsPII([PII("test")])).toBeTruthy()
2122
expect(containsPII({ test: PII(1) })).toBeTruthy()
2223
})
24+
25+
it("should return true after max depth", () => {
26+
expect(containsPII({ test: [{ hello: "world" }] }, 2)).toBeTruthy()
27+
})
2328
})

src/__tests__/detect.spec.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
import { detect, isPII } from "../index"
1+
import { isPII } from "../pii"
2+
import detect from "../detect"
3+
import unwrap from "../unwrap"
34

45
const detector = (data: unknown) => Array.isArray(data)
56

@@ -25,4 +26,10 @@ describe("detect", () => {
2526
const detectedArrays = detect(detector, { test: set })
2627
expect(isPII(Array.from((detectedArrays as any).test)[0])).toBeTruthy()
2728
})
29+
30+
it("should return PII after max depth", () => {
31+
const result: any = detect(() => false, { test: [{ hello: "world" }] }, 2)
32+
33+
expect(unwrap(result.test[0])).toEqual({ hello: "world" })
34+
})
2835
})

src/__tests__/fold.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { PII, fold, unwrap } from "../index"
1+
import PII from "../pii"
2+
import fold from "../fold"
3+
import unwrap from "../unwrap"
24

35
describe("fold", () => {
46
it("should fold multiple PII", () => {

src/__tests__/map.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { PII, map, unwrap } from "../index"
1+
import PII from "../pii"
2+
import map from "../map"
3+
import unwrap from "../unwrap"
24

35
describe("map", () => {
46
it("should map inside PII", () => {

src/__tests__/redact.spec.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { PII, redact } from "../index"
1+
import PII from "../pii"
2+
import redact from "../redact"
23

34
const REDACTED = "REDACTED"
45
const redactor = () => REDACTED
@@ -62,4 +63,10 @@ describe("redact", () => {
6263
two: REDACTED,
6364
})
6465
})
66+
67+
it("should return PII after max depth", () => {
68+
expect(redact(redactor, { test: [{ hello: "world" }] }, 2)).toEqual({
69+
test: [REDACTED],
70+
})
71+
})
6572
})

src/__tests__/tap.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { PII, tap, unwrap } from "../index"
1+
import PII from "../pii"
2+
import tap from "../tap"
3+
import unwrap from "../unwrap"
24

35
describe("tap", () => {
46
it("should tap inside PII", () => {

src/__tests__/test.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { PII, test } from "../index"
1+
import PII from "../pii"
2+
import test from "../test"
23

34
describe("test", () => {
45
it("should test predicate against PII", () => {

0 commit comments

Comments
 (0)