Skip to content

Commit 8fcde9c

Browse files
authored
Merge pull request #3 from jsr-core/add-attempt
feat(attempt): add attempt function
2 parents 7b046e7 + 592bc89 commit 8fcde9c

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,26 @@
66

77
A utility pack for handling error.
88

9+
## attempt
10+
11+
`attempt` is a function that executes a function and returns the result
12+
(`[error: unknown, value: T]`). If the function is successful, it returns
13+
`[undefined, value]`. If the function throws an error, it returns
14+
`[error, undefined]`.
15+
16+
```ts
17+
import { assertEquals } from "@std/assert";
18+
import { attempt } from "@core/errorutil/attempt";
19+
20+
assertEquals(attempt(() => 42), [undefined, 42]);
21+
assertEquals(
22+
attempt(() => {
23+
throw "err";
24+
}),
25+
["err", undefined],
26+
);
27+
```
28+
929
## ErrorObject
1030

1131
`ErrorObject` is a class that wraps an error object for serialization. It is

attempt.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export type Success<T> = [error: undefined, value: T];
2+
export type Failure<E> = [error: E, value: undefined];
3+
export type Result<T, E> = Success<T> | Failure<E>;
4+
5+
/**
6+
* Attempt to execute a function and return a Result<T, E>.
7+
*
8+
* @param fn - The function to execute.
9+
* @returns A Result<T, E> where T is the return type of the function and E is the error type.
10+
*
11+
* @example
12+
* ```ts
13+
* import { attempt } from "@core/errorutil/attempt";
14+
*
15+
* console.log(attempt(() => 1)); // [undefined, 1]
16+
* console.log(attempt(() => { throw "err" })); // ["err", undefined]
17+
* ```
18+
*/
19+
export function attempt<T, E>(fn: () => T): Result<T, E> {
20+
try {
21+
return [undefined, fn()];
22+
} catch (e) {
23+
return [e, undefined];
24+
}
25+
}

attempt_test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { test } from "@cross/test";
2+
import { assertEquals } from "@std/assert";
3+
import { attempt } from "./attempt.ts";
4+
5+
test("attempt should return a Success<T> when the function is successful", () => {
6+
const result = attempt(() => 1);
7+
assertEquals(result, [undefined, 1]);
8+
});
9+
10+
test("attempt should return a Failure<E> when the function is failed", () => {
11+
const result = attempt(() => {
12+
throw "err";
13+
});
14+
assertEquals(result, ["err", undefined]);
15+
});

deno.jsonc

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "0.0.0",
44
"exports": {
55
".": "./mod.ts",
6+
"./attempt": "./attempt.ts",
67
"./error-object": "./error_object.ts",
78
"./raise": "./raise.ts",
89
"./try-or": "./try_or.ts",
@@ -20,12 +21,14 @@
2021
"LICENSE"
2122
],
2223
"exclude": [
24+
"**/*_bench.ts",
2325
"**/*_test.ts",
2426
".*"
2527
]
2628
},
2729
"imports": {
2830
"@core/errorutil": "./mod.ts",
31+
"@core/errorutil/attempt": "./attempt.ts",
2932
"@core/errorutil/error-object": "./error_object.ts",
3033
"@core/errorutil/raise": "./raise.ts",
3134
"@core/errorutil/try-or": "./try_or.ts",

mod.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from "./attempt.ts";
12
export * from "./error_object.ts";
23
export * from "./raise.ts";
34
export * from "./try_or.ts";

0 commit comments

Comments
 (0)