diff options
Diffstat (limited to 'utils/either.ts')
-rw-r--r-- | utils/either.ts | 48 |
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); - } -} |