-
-
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.
Merge pull request #3 from jsr-core/add-attempt
feat(attempt): add attempt function
- Loading branch information
Showing
5 changed files
with
64 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
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,25 @@ | ||
export type Success<T> = [error: undefined, value: T]; | ||
export type Failure<E> = [error: E, value: undefined]; | ||
export type Result<T, E> = Success<T> | Failure<E>; | ||
|
||
/** | ||
* Attempt to execute a function and return a Result<T, E>. | ||
* | ||
* @param fn - The function to execute. | ||
* @returns A Result<T, E> where T is the return type of the function and E is the error type. | ||
* | ||
* @example | ||
* ```ts | ||
* import { attempt } from "@core/errorutil/attempt"; | ||
* | ||
* console.log(attempt(() => 1)); // [undefined, 1] | ||
* console.log(attempt(() => { throw "err" })); // ["err", undefined] | ||
* ``` | ||
*/ | ||
export function attempt<T, E>(fn: () => T): Result<T, E> { | ||
try { | ||
return [undefined, fn()]; | ||
} catch (e) { | ||
return [e, undefined]; | ||
} | ||
} |
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,15 @@ | ||
import { test } from "@cross/test"; | ||
import { assertEquals } from "@std/assert"; | ||
import { attempt } from "./attempt.ts"; | ||
|
||
test("attempt should return a Success<T> when the function is successful", () => { | ||
const result = attempt(() => 1); | ||
assertEquals(result, [undefined, 1]); | ||
}); | ||
|
||
test("attempt should return a Failure<E> when the function is failed", () => { | ||
const result = attempt(() => { | ||
throw "err"; | ||
}); | ||
assertEquals(result, ["err", undefined]); | ||
}); |
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