summaryrefslogtreecommitdiff
path: root/utils/either.ts
diff options
context:
space:
mode:
Diffstat (limited to 'utils/either.ts')
-rw-r--r--utils/either.ts25
1 files changed, 25 insertions, 0 deletions
diff --git a/utils/either.ts b/utils/either.ts
new file mode 100644
index 0000000..d21c796
--- /dev/null
+++ b/utils/either.ts
@@ -0,0 +1,25 @@
+export interface IEither<E, T> {
+ ok?: T;
+ err?: E;
+ mapBoth: <Ee, Tt>(
+ errBranch: (e: E) => Ee,
+ okBranch: (o: T) => Tt,
+ ) => IEither<Ee, Tt>;
+}
+
+export class Either<E, T> implements IEither<E, T> {
+ private constructor(readonly err?: E, readonly ok?: T) {}
+
+ public mapBoth<Ee, Tt>(errBranch: (e: E) => Ee, okBranch: (t: T) => Tt) {
+ if (this.err) return new Either<Ee, Tt>(errBranch(this.err));
+ return new Either<Ee, Tt>(undefined, okBranch(this.ok!));
+ }
+
+ static left<E, T>(e: E) {
+ return new Either<E, T>(e);
+ }
+
+ static right<E, T>(t: T) {
+ return new Either<E, T>(undefined, t);
+ }
+}