summaryrefslogtreecommitdiff
path: root/utils/either.ts
diff options
context:
space:
mode:
authorElizabeth Alexander Hunt <me@liz.coffee>2025-05-12 23:05:27 -0700
committerElizabeth Alexander Hunt <me@liz.coffee>2025-05-12 23:33:52 -0700
commite49fda41176d025a671802be76c219d66167276f (patch)
tree6791f0ffe26b2405e13436a7e0c8d05d60e2b3bc /utils/either.ts
parentbbaea13ee7125a9d289a74f0c173e7e75177e53c (diff)
downloadci-e49fda41176d025a671802be76c219d66167276f.tar.gz
ci-e49fda41176d025a671802be76c219d66167276f.zip
snapshot
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);
+ }
+}