This repository was archived by the owner on Apr 2, 2023. It is now read-only.
File tree 2 files changed +11
-38
lines changed
2 files changed +11
-38
lines changed Original file line number Diff line number Diff line change 1
1
import { PIITry , unwrap } from "../index"
2
- import { identity } from "../result"
3
2
4
3
describe ( "PIITry" , ( ) => {
5
4
it ( "should wrap results in PII" , async ( ) => {
6
5
const result = await PIITry ( ( ) => "TEST_STRING" )
7
- const success = result . fold ( identity , ( ) => void 0 )
8
- expect ( unwrap ( success ) ) . toBe ( "TEST_STRING" )
6
+ expect ( result ) . toBe ( "TEST_STRING" )
9
7
} )
10
8
11
9
it ( "should wrap exceptions in PII" , async ( ) => {
12
- const result = await PIITry < never , Error > ( ( ) => {
13
- throw new Error ( "Error message" )
14
- } )
15
-
16
- const errorMessage = result . fold (
17
- ( ) => "never" ,
18
- e => unwrap ( e ) . message ,
19
- )
20
-
21
- expect ( errorMessage ) . toBe ( "Error message" )
10
+ try {
11
+ await PIITry ( ( ) => {
12
+ throw new Error ( "Error message" )
13
+ } )
14
+ } catch ( e ) {
15
+ expect ( unwrap ( e ) . message ) . toBe ( "Error message" )
16
+ }
22
17
} )
23
18
} )
Original file line number Diff line number Diff line change @@ -2,32 +2,10 @@ import { PII } from "./pii"
2
2
3
3
export const identity = < X > ( x : X ) : X => x
4
4
5
- export class Ok < S , E > {
6
- constructor ( private value : S ) { }
7
-
8
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
9
- fold < R > ( ok : ( value : S ) => R , _err : ( err : E ) => R ) : R {
10
- return ok ( this . value )
11
- }
12
- }
13
-
14
- export class Err < E , S > {
15
- constructor ( private err : E ) { }
16
-
17
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
18
- fold < R > ( _ok : ( value : S ) => R , err : ( err : E ) => R ) : R {
19
- return err ( this . err )
20
- }
21
- }
22
-
23
- export type Result < S , E > = Ok < S , E > | Err < E , S >
24
-
25
- export async function PIITry < S , E extends Error > (
26
- fn : ( ) => S | Promise < S > ,
27
- ) : Promise < Result < PII < S > , PII < E > > > {
5
+ export async function PIITry < S > ( fn : ( ) => S | Promise < S > ) : Promise < S > {
28
6
try {
29
- return new Ok ( PII ( await fn ( ) ) )
7
+ return fn ( )
30
8
} catch ( e ) {
31
- return new Err ( PII ( e ) )
9
+ throw PII ( e )
32
10
}
33
11
}
You can’t perform that action at this time.
0 commit comments