From 2543ac8b11af11f034836591046cdb52911f9403 Mon Sep 17 00:00:00 2001 From: Elizabeth Alexander Hunt Date: Tue, 13 May 2025 09:50:15 -0700 Subject: snapshot --- utils/either.ts | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'utils/either.ts') diff --git a/utils/either.ts b/utils/either.ts index d21c796..10e4f43 100644 --- a/utils/either.ts +++ b/utils/either.ts @@ -5,14 +5,37 @@ export interface IEither { errBranch: (e: E) => Ee, okBranch: (o: T) => Tt, ) => IEither; + mapRight: (mapper: (t: T) => Tt) => Either; + mapLeft: (mapper: (e: E) => Ee) => Either; + flatMap: ( + mapper: (e: T) => Either, + ) => Either; } export class Either implements IEither { private constructor(readonly err?: E, readonly ok?: T) {} - public mapBoth(errBranch: (e: E) => Ee, okBranch: (t: T) => Tt) { - if (this.err) return new Either(errBranch(this.err)); - return new Either(undefined, okBranch(this.ok!)); + public mapBoth( + errBranch: (e: E) => Ee, + okBranch: (t: T) => Tt, + ): Either { + if (this.err) return Either.left(errBranch(this.err)); + return Either.right(okBranch(this.ok!)); + } + + public flatMap(mapper: (t: T) => Either) { + if (this.ok) return mapper(this.ok); + return this; + } + + public mapRight(mapper: (t: T) => Tt): Either { + if (this.ok) return Either.right(mapper(this.ok)); + return Either.left(this.err!); + } + + public mapLeft(mapper: (e: E) => Ee): Either { + if (this.err) return Either.left(mapper(this.err)); + return Either.right(this.ok!); } static left(e: E) { -- cgit v1.2.3-70-g09d2