Skip to content

Commit 3609529

Browse files
committed
refactor: decrease code size
1 parent 0f2bca8 commit 3609529

File tree

2 files changed

+18
-30
lines changed

2 files changed

+18
-30
lines changed

src/result.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,7 @@ export function okOrThrow<T, E>(result: Result<T, E>): T {
116116
if (isOk(result)) {
117117
return result.value;
118118
}
119-
if (isErr(result)) {
120-
throw result.error;
121-
}
122-
throw new Error("Invalid result type");
119+
throw (result as unknown as { _tag: "Err"; error: E }).error;
123120
}
124121

125122
/**
@@ -176,8 +173,5 @@ export function match<T, E, U>(
176173
if (isOk(result)) {
177174
return handlers.ok(result.value);
178175
}
179-
if (isErr(result)) {
180-
return handlers.err(result.error);
181-
}
182-
throw new Error("Invalid result type");
176+
return handlers.err((result as unknown as { _tag: "Err"; error: E }).error);
183177
}

src/try.ts

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { err, ok, type Result } from "./result";
1+
import { err, isErr, ok, type Result } from "./result";
22

33
/**
44
* Run a function, catching whatever's thrown and return a Result.
@@ -35,13 +35,11 @@ export function tryUnknownFn<T, E = unknown>(
3535
fn: () => T | Promise<T>,
3636
): Result<T, E> | Promise<Result<T, E>> {
3737
try {
38-
const result = fn();
39-
if (result instanceof Promise) {
40-
return result
41-
.then((value) => ok(value))
42-
.catch((error) => err(error as E));
38+
const r = fn();
39+
if (r instanceof Promise) {
40+
return r.then((value) => ok(value)).catch((error) => err(error as E));
4341
}
44-
return ok(result as T);
42+
return ok(r as T);
4543
} catch (error) {
4644
return err(error as E);
4745
}
@@ -75,31 +73,27 @@ export function tryFn<T, E = Error>(fn: () => T): Result<T, E>;
7573
export function tryFn<T, E = Error>(
7674
fn: () => T | Promise<T>,
7775
): Result<T, E> | Promise<Result<T, E>> {
78-
const result = tryUnknownFn(fn);
76+
const r = tryUnknownFn(fn);
7977

80-
if (result instanceof Promise) {
81-
return result.then((res) => {
82-
if (res._tag === "Ok") {
83-
return res;
84-
}
85-
if (res._tag === "Err") {
78+
if (r instanceof Promise) {
79+
return r.then((res) => {
80+
if (isErr(res)) {
8681
if (res.error instanceof Error) {
8782
return err(res.error as E);
8883
}
8984
return err(new Error(String(res.error)) as E);
9085
}
86+
9187
return res;
9288
});
9389
}
9490

95-
if (result._tag === "Ok") {
96-
return result as Result<T, E>;
97-
}
98-
if (result._tag === "Err") {
99-
if (result.error instanceof Error) {
100-
return err(result.error as E);
91+
if (isErr(r)) {
92+
if (r.error instanceof Error) {
93+
return err(r.error as E);
10194
}
102-
return err(new Error(String(result.error)) as E);
95+
return err(new Error(String(r.error)) as E);
10396
}
104-
return result as Result<T, E>;
97+
98+
return r as Result<T, E>;
10599
}

0 commit comments

Comments
 (0)