Skip to content

Commit cf32c39

Browse files
committed
feat: adds equals and extract methods to Result
1 parent 674d7e3 commit cf32c39

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

src/result/Result.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ export interface Result<T, E> {
77
chain<U>(fn: (value: T) => Result<U, E>): Result<U, E>;
88
unwrapOr(defaultValue: T): T;
99
match<U>(cases: { ok: (value: T) => U; err: (err: E) => U }): U;
10+
equals(other: Result<T, E>): boolean;
11+
12+
extract(): T | E;
1013
}
1114

1215
export class Ok<T, E> implements Result<T, E> {
@@ -39,6 +42,14 @@ export class Ok<T, E> implements Result<T, E> {
3942
match<U>(cases: { ok: (value: T) => U; err: (err: E) => U }): U {
4043
return cases.ok(this.value);
4144
}
45+
46+
equals(other: Result<T, E>): boolean {
47+
return other.isOk() ? this.value === other.extract() : false;
48+
}
49+
50+
extract(): T {
51+
return this.value;
52+
}
4253
}
4354

4455
export class Err<T, E> implements Result<T, E> {
@@ -71,6 +82,14 @@ export class Err<T, E> implements Result<T, E> {
7182
match<U>(cases: { ok: (value: T) => U; err: (err: E) => U }): U {
7283
return cases.err(this.error);
7384
}
85+
86+
equals(other: Result<T, E>): boolean {
87+
return other.isErr() ? this.error === other.extract() : false;
88+
}
89+
90+
extract(): E {
91+
return this.error;
92+
}
7493
}
7594

7695
export const fromThrowable = <T, E = unknown>(
@@ -137,3 +156,9 @@ export const matchR = <T, E, U>(
137156
) => (result: Result<T, E>): U => {
138157
return result.match(cases);
139158
};
159+
160+
export const equalsR = <T, E>(
161+
other: Result<T, E>
162+
) => (result: Result<T, E>): boolean => {
163+
return result.equals(other);
164+
};

tests/result.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { pipe } from "rambda";
22
import { Ok, Err, fromThrowable, fromPromise, fromAsync, matchR, mapR, unwrapOrR, chainR, mapErrR } from "../src/result";
3+
import { equalsR } from "../src/result/Result";
34

45
describe("Result", () => {
56
it("Ok.map should apply the function", () => {
@@ -69,6 +70,30 @@ describe("Result", () => {
6970
expect(result.isErr()).toBe(true);
7071
expect(result.isOk()).toBe(false);
7172
});
73+
74+
it("Ok.equals should be equal to another Ok with same value", () => {
75+
const result1 = new Ok(5);
76+
const result2 = new Ok(5);
77+
expect(result1.equals(result2)).toBe(true);
78+
});
79+
80+
it("Err.equals should be equal to another Err with same error", () => {
81+
const result1 = new Err("error");
82+
const result2 = new Err("error");
83+
expect(result1.equals(result2)).toBe(true);
84+
});
85+
86+
it("Ok.equals should not be equal to Err", () => {
87+
const result1 = new Ok(5);
88+
const result2 = new Err<number, string>("error");
89+
expect(result1.equals(result2)).toBe(false);
90+
});
91+
92+
it("Err.equals should not be equal to Ok", () => {
93+
const result1 = new Err("error");
94+
const result2 = new Ok<number, string>(5);
95+
expect(result1.equals(result2)).toBe(false);
96+
});
7297
});
7398

7499
describe("Result fromThrowable", () => {
@@ -257,4 +282,36 @@ describe("Result - Curried Helpers", () => {
257282
);
258283
expect(result).toBe("Error: Something went wrong");
259284
});
285+
286+
it("should equals with another Ok using curried equals", () => {
287+
const result = pipe(
288+
new Ok(5),
289+
equalsR(new Ok(5))
290+
);
291+
expect(result).toBe(true);
292+
});
293+
294+
it("should not equals with another Ok using curried equals", () => {
295+
const result = pipe(
296+
new Ok(5),
297+
equalsR(new Ok(10))
298+
);
299+
expect(result).toBe(false);
300+
});
301+
302+
it("should equals with another Err using curried equals", () => {
303+
const result = pipe(
304+
new Err("Error"),
305+
equalsR(new Err("Error"))
306+
);
307+
expect(result).toBe(true);
308+
});
309+
310+
it("should not equals with another Err using curried equals", () => {
311+
const result = pipe(
312+
new Err("Error"),
313+
equalsR(new Err("Different Error"))
314+
);
315+
expect(result).toBe(false);
316+
});
260317
});

0 commit comments

Comments
 (0)