summaryrefslogtreecommitdiff
path: root/utils/either.ts
diff options
context:
space:
mode:
authorElizabeth Hunt <lizhunt@amazon.com>2025-05-13 18:58:45 -0700
committerElizabeth Hunt <lizhunt@amazon.com>2025-05-13 18:58:54 -0700
commit1d66a0f58e4ebcdf4f42c9d78f82a1ab49a2cf11 (patch)
tree07073c060b61688e4635fd4658315cc683589d3d /utils/either.ts
parent2543ac8b11af11f034836591046cdb52911f9403 (diff)
downloadci-1d66a0f58e4ebcdf4f42c9d78f82a1ab49a2cf11.tar.gz
ci-1d66a0f58e4ebcdf4f42c9d78f82a1ab49a2cf11.zip
snapshot!
Diffstat (limited to 'utils/either.ts')
-rw-r--r--utils/either.ts48
1 files changed, 0 insertions, 48 deletions
diff --git a/utils/either.ts b/utils/either.ts
deleted file mode 100644
index 10e4f43..0000000
--- a/utils/either.ts
+++ /dev/null
@@ -1,48 +0,0 @@
-export interface IEither<E, T> {
- ok?: T;
- err?: E;
- mapBoth: <Ee, Tt>(
- errBranch: (e: E) => Ee,
- okBranch: (o: T) => Tt,
- ) => IEither<Ee, Tt>;
- mapRight: <Tt>(mapper: (t: T) => Tt) => Either<E, Tt>;
- mapLeft: <Ee>(mapper: (e: E) => Ee) => Either<Ee, T>;
- flatMap: <Ee extends E, Tt>(
- mapper: (e: T) => Either<Ee, Tt>,
- ) => Either<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,
- ): Either<Ee, Tt> {
- if (this.err) return Either.left(errBranch(this.err));
- return Either.right(okBranch(this.ok!));
- }
-
- public flatMap<Ee extends E, Tt>(mapper: (t: T) => Either<Ee, Tt>) {
- if (this.ok) return mapper(this.ok);
- return this;
- }
-
- public mapRight<Tt>(mapper: (t: T) => Tt): Either<E, Tt> {
- if (this.ok) return Either.right(mapper(this.ok));
- return Either.left(this.err!);
- }
-
- public mapLeft<Ee>(mapper: (e: E) => Ee): Either<Ee, T> {
- if (this.err) return Either.left(mapper(this.err));
- return Either.right(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);
- }
-}