This repository has been archived by the owner on Apr 2, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add optional maxDepth option to recursive functions: containsPI…
…I, detect, redact, unwrapObject
- Loading branch information
Showing
32 changed files
with
329 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import PII from "./pii" | ||
|
||
export default async function PIITry<S>(fn: () => S | Promise<S>): Promise<S> { | ||
try { | ||
return fn() | ||
} catch (e) { | ||
throw PII(e, "REDACTED_ERROR") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { isPII } from "./pii" | ||
import visit from "./visit" | ||
|
||
const containsPII = (input: unknown, maxDepth = Infinity): boolean => | ||
maxDepth === 0 || isPII(input) | ||
? true | ||
: visit(input, { | ||
record: o => Object.values(o).some(i => containsPII(i, maxDepth - 1)), | ||
map: m => | ||
Array.from(m).some( | ||
([k, v]) => | ||
containsPII(k, maxDepth - 1) || containsPII(v, maxDepth - 1), | ||
), | ||
array: a => a.some(i => containsPII(i, maxDepth - 1)), | ||
set: s => Array.from(s).some(i => containsPII(i, maxDepth - 1)), | ||
primitive: p => isPII(p), | ||
object: p => isPII(p), | ||
}) | ||
|
||
export default containsPII |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import PII, { isPII } from "./pii" | ||
import visit from "./visit" | ||
|
||
const detect = ( | ||
detector: (data: unknown) => boolean, | ||
input: unknown, | ||
maxDepth = Infinity, | ||
): unknown => | ||
isPII(input) | ||
? input | ||
: detector(input) || maxDepth === 0 | ||
? PII(input) | ||
: visit(input, { | ||
record: r => | ||
Object.keys(r).reduce((sum, key) => { | ||
sum[key] = detect(detector, r[key], maxDepth - 1) | ||
return sum | ||
}, {} as Record<string, unknown>), | ||
map: m => | ||
new Map( | ||
Array.from(m).map(([k, v]) => [ | ||
detect(detector, k, maxDepth - 1), | ||
detect(detector, v, maxDepth - 1), | ||
]), | ||
), | ||
array: a => a.map(x => detect(detector, x, maxDepth - 1)), | ||
set: s => | ||
new Set(Array.from(s).map(x => detect(detector, x, maxDepth - 1))), | ||
primitive: p => p, | ||
object: o => o, | ||
}) | ||
|
||
export default detect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import PII from "./pii" | ||
import unwrap from "./unwrap" | ||
|
||
export default <A, B>( | ||
fn: ( | ||
previousValue: B, | ||
currentValue: A, | ||
currentIndex: number, | ||
array: A[], | ||
) => B, | ||
initial: B, | ||
a: Array<PII<A> | A>, | ||
): PII<B> => PII(a.map<A>(unwrap).reduce(fn, initial)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,14 @@ | ||
export * from "./pii" | ||
export * from "./result" | ||
export { default as containsPII } from "./containsPII" | ||
export { default as detect } from "./detect" | ||
export { default as fold } from "./fold" | ||
export { default as map } from "./map" | ||
export { default as PII, isPII } from "./pii" | ||
export { default as PIITry } from "./PIITry" | ||
export { default as redact } from "./redact" | ||
export { default as tap } from "./tap" | ||
export { default as test } from "./test" | ||
export { default as unwrap } from "./unwrap" | ||
export { default as unwrapObject } from "./unwrapObject" | ||
export { default as zip2With } from "./zip2With" | ||
export { default as zip3With } from "./zip3With" | ||
export { default as zip4With } from "./zip4With" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Function, regex, object, Number, String, etc | ||
export default (value: unknown): boolean => { | ||
const type = typeof value | ||
return value != null && (type == "object" || type == "function") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const proto = Object.prototype | ||
const gpo = Object.getPrototypeOf | ||
|
||
export default (obj: unknown): obj is Record<string, unknown> => | ||
obj === null || typeof obj !== "object" ? false : gpo(obj) === proto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import PII from "./pii" | ||
import unwrap from "./unwrap" | ||
|
||
function map<T, T2>(fn: (item: T) => T2, item: PII<T>): PII<T2> | ||
function map<T, T2>(fn: (item: T) => T2, item: T): Exclude<T2, PII<any>> | ||
function map<T, T2>( | ||
fn: (item: T) => T2, | ||
item: PII<T> | T, | ||
): PII<T2> | Exclude<T2, PII<any>> { | ||
return PII(fn(unwrap(item))) | ||
} | ||
|
||
export default map |
Oops, something went wrong.