Skip to content

Commit 831e77b

Browse files
committed
feat: move unwrap out of try class
1 parent e7442c6 commit 831e77b

2 files changed

Lines changed: 17 additions & 26 deletions

File tree

src/try.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ test("new Try()", () => {
55
const t = new Try(() => true);
66
expect(t.ok).toBe(true);
77
expect(Try.isTry(t)).toBe(true);
8-
expect(t.unwrap()).toBe(true);
8+
expect(Try.unwrap(t)).toBe(true);
99
});
1010

1111
test("Try()", () => {
1212
const t = Try(() => true);
1313
expect(t.ok).toBe(true);
1414
expect(Try.isTry(t)).toBe(true);
15-
expect(t.unwrap()).toBe(true);
15+
expect(Try.unwrap(t)).toBe(true);
1616
});
1717

1818
test("Try.success()", () => {
@@ -22,7 +22,7 @@ test("Try.success()", () => {
2222
expect(Try.isError(t)).toBe(false);
2323
expect(Try.isTry(t)).toBe(true);
2424
expect(t).toBeInstanceOf(Success);
25-
expect(t.unwrap()).toBe("pass");
25+
expect(Try.unwrap(t)).toBe("pass");
2626
});
2727

2828
test("Try.failure()", () => {
@@ -32,5 +32,5 @@ test("Try.failure()", () => {
3232
expect(Try.isError(t)).toBe(true);
3333
expect(Try.isTry(t)).toBe(true);
3434
expect(t).toBeInstanceOf(Failure);
35-
expect(() => t.unwrap()).toThrowError("fail");
35+
expect(() => Try.unwrap(t)).toThrowError("fail");
3636
});

src/try.ts

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ interface ITry<A> {
77
): Try<A | B>;
88
finally(onfinally?: (() => void) | undefined | null): Try<A>;
99

10-
unwrap(): A;
11-
result(): Result<A>;
12-
1310
readonly [Symbol.toStringTag]: string;
1411
}
1512

@@ -59,14 +56,6 @@ export class Success<A> implements ITry<A> {
5956
return new Failure(error);
6057
}
6158
}
62-
63-
unwrap(): A {
64-
return this.value;
65-
}
66-
67-
result(): Result<A> {
68-
return { ok: true, value: this.value };
69-
}
7059
}
7160

7261
export class Failure<A> implements ITry<A> {
@@ -115,14 +104,6 @@ export class Failure<A> implements ITry<A> {
115104
return new Failure(error);
116105
}
117106
}
118-
119-
unwrap(): A {
120-
throw this.error;
121-
}
122-
123-
result(): Result<A> {
124-
return { ok: false, error: this.error };
125-
}
126107
}
127108

128109
export type Try<A> = Success<A> | Failure<A>;
@@ -135,6 +116,7 @@ export interface TryConstructor {
135116
apply<A>(value: () => A): Try<A>;
136117
success<A>(value: A | Try<A>): Try<Unwrapped<A>>;
137118
failure<A = never>(reason: unknown): Try<A>;
119+
unwrap<A>(value: Try<A>): A;
138120
isOk(value: unknown): value is Success<unknown>;
139121
isError(value: unknown): value is Failure<unknown>;
140122
isTry(value: unknown): value is Try<unknown>;
@@ -156,18 +138,27 @@ const TryImplementation: Omit<TryConstructor, never> = {
156138
success<A>(value: A | Try<A>): Try<Unwrapped<A>> {
157139
if (TryImplementation.isTry(value)) {
158140
return TryImplementation.apply(
159-
value.unwrap.bind(value)
160-
) as unknown as Try<Unwrapped<A>>;
141+
() => TryImplementation.unwrap(value) as Unwrapped<A>
142+
);
161143
}
162144
return new Success(value) as unknown as Try<Unwrapped<A>>;
163145
},
164146
failure<A = never>(reason: unknown): Try<A> {
165147
if (TryImplementation.isTry(reason)) {
166-
return TryImplementation.apply(reason.unwrap.bind(reason) as () => A);
148+
return TryImplementation.apply(
149+
() => TryImplementation.unwrap(reason) as A
150+
);
167151
}
168152

169153
return new Failure(reason);
170154
},
155+
unwrap<A>(value: Try<A>): A {
156+
if (TryImplementation.isOk(value)) {
157+
return value.value;
158+
} else {
159+
throw value.error;
160+
}
161+
},
171162
isOk(value: unknown): value is Success<unknown> {
172163
return value instanceof Success;
173164
},

0 commit comments

Comments
 (0)