Skip to content

Commit 674d7e3

Browse files
committed
feat: adds equality checks and extraction methods to Maybe
1 parent d1c40a9 commit 674d7e3

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

src/maybe/Maybe.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ export interface Maybe<T> {
66
chain<U>(fn: (value: T) => Maybe<U>): Maybe<U>;
77
unwrapOr(defaultValue: T): T;
88
match<U>(cases: { just: (value: T) => U; nothing: () => U }): U;
9+
equals(other: Maybe<T>): boolean;
10+
11+
extract(): T;
912
}
1013

1114
export class Just<T> implements Maybe<T> {
12-
constructor(private readonly value: T) {}
15+
constructor(private readonly value: T) { }
1316

1417
isJust(): boolean {
1518
return true;
@@ -34,6 +37,14 @@ export class Just<T> implements Maybe<T> {
3437
match<U>(cases: { just: (value: T) => U; nothing: () => U }): U {
3538
return cases.just(this.value);
3639
}
40+
41+
equals(other: Maybe<T>): boolean {
42+
return other.isJust() ? this.value === other.extract() : false;
43+
}
44+
45+
extract(): T {
46+
return this.value;
47+
}
3748
}
3849

3950
export class Nothing<T> implements Maybe<T> {
@@ -60,6 +71,14 @@ export class Nothing<T> implements Maybe<T> {
6071
match<U>(cases: { just: (value: T) => U; nothing: () => U }): U {
6172
return cases.nothing();
6273
}
74+
75+
equals(other: Maybe<T>): boolean {
76+
return other.isNothing();
77+
}
78+
79+
extract(): T {
80+
return undefined as T;
81+
}
6382
}
6483

6584
export const fromNullable = <T>(value: T | null | undefined): Maybe<T> => {
@@ -87,3 +106,7 @@ export const matchM = <T, U>(
87106
) => (maybe: Maybe<T>): U => {
88107
return maybe.match(cases);
89108
};
109+
110+
export const equalsM = <T>(other: Maybe<T>) => (maybe: Maybe<T>): boolean => {
111+
return maybe.equals(other);
112+
};

tests/maybe.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { pipe } from "rambda";
22
import { Just, Nothing, fromNullable, chainM, mapM, unwrapOrM, matchM } from "../src/maybe";
3+
import { equalsM } from "../src/maybe/Maybe";
34

45
describe("Maybe", () => {
56
it("should map over Just", () => {
@@ -45,6 +46,28 @@ describe("Maybe", () => {
4546
expect(justMsg).toBe("Temos value");
4647
expect(nothingMsg).toBe("Nada aqui");
4748
});
49+
50+
it("should check equality between Just and Nothing", () => {
51+
const just1 = new Just(10);
52+
const just2 = new Just(10);
53+
const nothing1 = new Nothing<number>();
54+
const nothing2 = new Nothing<number>();
55+
56+
expect(just1.equals(just2)).toBe(true);
57+
expect(just1.equals(nothing1)).toBe(false);
58+
expect(nothing1.equals(nothing2)).toBe(true);
59+
});
60+
61+
it("should check equality with different values", () => {
62+
const just1 = new Just(10);
63+
const just2 = new Just(20);
64+
const nothing1 = new Nothing<number>();
65+
const nothing2 = new Nothing<number>();
66+
67+
expect(just1.equals(just2)).toBe(false);
68+
expect(just1.equals(nothing1)).toBe(false);
69+
expect(nothing1.equals(nothing2)).toBe(true);
70+
});
4871
});
4972

5073
describe("Maybe fromNullable", () => {
@@ -134,6 +157,29 @@ describe("Maybe - Curried Helpers", () => {
134157
);
135158
expect(result).toBe("Nada aqui");
136159
});
160+
161+
it("should check equality with curried equals", () => {
162+
const just1 = new Just(10);
163+
const just2 = new Just(10);
164+
const nothing1 = new Nothing<number>();
165+
const nothing2 = new Nothing<number>();
166+
167+
expect(pipe(just1, equalsM(just2))).toBe(true);
168+
expect(pipe(just1, equalsM(nothing1))).toBe(false);
169+
expect(pipe(nothing1, equalsM(nothing2))).toBe(true);
170+
});
171+
172+
it("should check extract value from Just", () => {
173+
const just = new Just(10);
174+
const result = just.extract();
175+
expect(result).toBe(10);
176+
});
177+
178+
it("should check extract value from Nothing", () => {
179+
const nothing = new Nothing<number>();
180+
const result = nothing.extract();
181+
expect(result).toBe(undefined);
182+
});
137183
});
138184

139185

0 commit comments

Comments
 (0)