Skip to content

Commit 709f27f

Browse files
committed
feat: add alter and alterElse functions
1 parent 8fcde9c commit 709f27f

6 files changed

+106
-0
lines changed

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,30 @@
66

77
A utility pack for handling error.
88

9+
## alter / alterElse
10+
11+
`alter` and `alterElse` are functions that execute a function and return the
12+
result. If the function throws an error, `alter` throws the given error, and
13+
`alterElse` throws the result of the second function.
14+
15+
```ts
16+
import { assertThrows } from "@std/assert";
17+
import { alter } from "@core/errorutil/alter";
18+
import { alterElse } from "@core/errorutil/alter-else";
19+
20+
const fn = () => {
21+
throw new Error("This is an error message");
22+
};
23+
24+
assertThrows(() => alter(fn, new Error("custom error")), Error, "custom error");
25+
26+
assertThrows(
27+
() => alterElse(fn, () => new Error("custom error")),
28+
Error,
29+
"custom error",
30+
);
31+
```
32+
933
## attempt
1034

1135
`attempt` is a function that executes a function and returns the result

alter.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Alter the error of a function if an error is thrown.
3+
*
4+
* @param fn - The function to execute.
5+
* @param alt - The value to throw if an error is thrown.
6+
* @returns The result of the function.
7+
* @throws The value of alt.
8+
* @example
9+
*
10+
* ```ts
11+
* import { alter } from "@core/errorutil/alter";
12+
*
13+
* console.log(alter(() => 1, "err2")); // 1
14+
* console.log(alter(() => { throw "err1" }, "err2")); // "err2" is thrown
15+
* ```
16+
*/
17+
export function alter<T, E>(fn: () => T, alt: E): T {
18+
try {
19+
return fn();
20+
} catch {
21+
throw alt;
22+
}
23+
}

alter_else.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Alter the error of a function if an error is thrown.
3+
*
4+
* @param fn - The function to execute.
5+
* @param modifier - The function to execute if an error is thrown.
6+
* @returns The result of the function.
7+
* @throws The result of the modifier function.
8+
* @example
9+
*
10+
* ```ts
11+
* import { alterElse } from "@core/errorutil/alter-else";
12+
*
13+
* console.log(alterElse(() => 1, () => "err")); // 1
14+
* console.log(alterElse(() => { throw "err" }, (err) => "new " + err)); // "new err" is thrown
15+
* ```
16+
*/
17+
export function alterElse<T, E>(fn: () => T, elseFn: (err: unknown) => E): T {
18+
try {
19+
return fn();
20+
} catch (err) {
21+
throw elseFn(err);
22+
}
23+
}

alter_else_test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { test } from "@cross/test";
2+
import { assertEquals, assertThrows } from "@std/assert";
3+
import { raise } from "./raise.ts";
4+
import { alterElse } from "./alter_else.ts";
5+
6+
test("alterElse should return a function result", () => {
7+
assertEquals(alterElse(() => 1, () => "err2"), 1);
8+
});
9+
10+
test("alterElse should throws an alt when the function throws error", () => {
11+
assertThrows(
12+
() => alterElse(() => raise("err"), (err) => new Error(`new ${err}`)),
13+
Error,
14+
"new err",
15+
);
16+
});

alter_test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { test } from "@cross/test";
2+
import { assertEquals, assertThrows } from "@std/assert";
3+
import { raise } from "./raise.ts";
4+
import { alter } from "./alter.ts";
5+
6+
test("alter should return a function result", () => {
7+
assertEquals(alter(() => 1, "err2"), 1);
8+
});
9+
10+
test("alter should throws an alt when the function throws error", () => {
11+
assertThrows(
12+
() => alter(() => raise("err1"), new Error("err2")),
13+
Error,
14+
"err2",
15+
);
16+
});

deno.jsonc

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
"version": "0.0.0",
44
"exports": {
55
".": "./mod.ts",
6+
"./alter": "./alter.ts",
7+
"./alter-else": "./alter_else.ts",
68
"./attempt": "./attempt.ts",
79
"./error-object": "./error_object.ts",
810
"./raise": "./raise.ts",
@@ -28,6 +30,8 @@
2830
},
2931
"imports": {
3032
"@core/errorutil": "./mod.ts",
33+
"@core/errorutil/alter": "./alter.ts",
34+
"@core/errorutil/alter-else": "./alter_else.ts",
3135
"@core/errorutil/attempt": "./attempt.ts",
3236
"@core/errorutil/error-object": "./error_object.ts",
3337
"@core/errorutil/raise": "./raise.ts",

0 commit comments

Comments
 (0)