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.
- Loading branch information
Showing
3 changed files
with
57 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { PIITry, unwrap } from "../index" | ||
import { identity } from "../result" | ||
|
||
describe("PIITry", () => { | ||
it("should wrap results in PII", async () => { | ||
const result = await PIITry(() => "TEST_STRING") | ||
const success = result.fold(identity, () => void 0) | ||
expect(unwrap(success)).toBe("TEST_STRING") | ||
}) | ||
|
||
it("should wrap exceptions in PII", async () => { | ||
const result = await PIITry<never, Error>(() => { | ||
throw new Error("Error message") | ||
}) | ||
|
||
const errorMessage = result.fold( | ||
() => "never", | ||
e => unwrap(e).message, | ||
) | ||
|
||
expect(errorMessage).toBe("Error message") | ||
}) | ||
}) |
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 +1,2 @@ | ||
export * from "./pii" | ||
export * from "./result" |
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 } from "./pii" | ||
|
||
export const identity = <X>(x: X): X => x | ||
|
||
export class Ok<S, E> { | ||
constructor(private value: S) {} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
fold<R>(ok: (value: S) => R, _err: (err: E) => R): R { | ||
return ok(this.value) | ||
} | ||
} | ||
|
||
export class Err<E, S> { | ||
constructor(private err: E) {} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
fold<R>(_ok: (value: S) => R, err: (err: E) => R): R { | ||
return err(this.err) | ||
} | ||
} | ||
|
||
export type Result<S, E> = Ok<S, E> | Err<E, S> | ||
|
||
export async function PIITry<S, E extends Error>( | ||
fn: () => S | Promise<S>, | ||
): Promise<Result<PII<S>, PII<E>>> { | ||
try { | ||
return new Ok(PII(await fn())) | ||
} catch (e) { | ||
return new Err(PII(e)) | ||
} | ||
} |