summaryrefslogtreecommitdiff
path: root/utils/either.ts
blob: 10e4f4335737e18c1892cc441583aa2c21a66851 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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);
  }
}