UNPKG

@typed-f/either

Version:

[![NPM Version][either-npm-version-badge]][either-npm] [repo-circleci-badge]: https://img.shields.io/circleci/project/github/Ailrun/typed-f/master.svg?logo=circleci [![Known Vulnerabilities][either-snyk-badge]][either-snyk] [![Supported TypeScript Version

93 lines (92 loc) 3.51 kB
import { Fun } from '@typed-f/function'; import { MatchPatterns, Matchable } from '@typed-f/matchable'; import { Monad2 } from '@typed-f/monad'; import { Setoid2 } from '@typed-f/setoid'; declare type EitherTag = '__typed_f__Either__'; declare const EitherTag: EitherTag; declare module '@typed-f/tagged' { interface Tag2List<L, R> { [EitherTag]: Either<L, R>; } } export interface EitherPatterns<L, R, U> extends MatchPatterns<U> { left(l: L): U; right(r: R): U; } declare abstract class BaseEither<L, R> implements Matchable, Setoid2<EitherTag>, Monad2<EitherTag, L, R> { __typed_f__tag__: EitherTag; protected abstract _kind: Either.Kind; abstract isLeft(): this is Left<L, R>; abstract isRight(): this is Right<L, R>; abstract leftOr(def: L): L; abstract leftOrThrow(err: Error): L; abstract leftOrCompute(f: Fun<[R], L>): L; abstract rightOr(def: R): R; abstract rightOrThrow(err: Error): R; abstract rightOrCompute(f: Fun<[L], R>): R; abstract matchWith<U>(cases: EitherPatterns<L, R, U>): U; abstract equals(other: Either<any, any>): boolean; abstract map<U>(f: Fun<[R], U>): Either<L, U>; abstract ap<U>(f: Either<L, Fun<[R], U>>): Either<L, U>; abstract bind<U>(f: Fun<[R], Either<L, U>>): Either<L, U>; caseOf: <U>(cases: EitherPatterns<L, R, U>) => U; notEquals(other: Either<any, any>): boolean; lift: <U>(f: Fun<[R], U>) => Either<L, U>; fmap: <U>(f: Fun<[R], U>) => Either<L, U>; unit<U>(v: U): Either<any, U>; of: <U>(v: U) => Either<any, U>; chain: <U>(f: Fun<[R], Either<L, U>>) => Either<L, U>; } export declare class Left<L, R> extends BaseEither<L, R> { private readonly _value; protected _kind: Either.Kind.Left; get value(): L; constructor(value: L); isLeft(): this is Left<L, R>; isRight(): this is Right<L, R>; leftOr(_def: L): L; leftOrThrow(_err: Error): L; leftOrCompute(_f: Fun<[R], L>): L; rightOr(def: R): R; rightOrThrow(err: Error): R; rightOrCompute(f: Fun<[L], R>): R; matchWith<U>(cases: EitherPatterns<L, R, U>): U; equals(other: Either<any, any>): boolean; map<U>(_f: Fun<[R], U>): Either<L, U>; ap<U>(_f: Either<L, Fun<[R], U>>): Either<L, U>; bind<U>(_f: Fun<[R], Either<L, U>>): Either<L, U>; } export declare class Right<L, R> extends BaseEither<L, R> { private readonly _value; protected _kind: Either.Kind.Right; get value(): R; constructor(value: R); isLeft(): this is Left<L, R>; isRight(): this is Right<L, R>; leftOr(def: L): L; leftOrThrow(err: Error): L; leftOrCompute(f: Fun<[R], L>): L; rightOr(_def: R): R; rightOrThrow(_err: Error): R; rightOrCompute(_f: Fun<[L], R>): R; matchWith<U>(cases: EitherPatterns<L, R, U>): U; equals(other: Either<any, any>): boolean; map<U>(f: Fun<[R], U>): Either<L, U>; ap<U>(f: Either<L, Fun<[R], U>>): Either<L, U>; bind<U>(f: Fun<[R], Either<L, U>>): Either<L, U>; } export declare type Either<L, R> = Left<L, R> | Right<L, R>; export declare namespace Either { enum Kind { Left = 0, Right = 1 } function unit<R>(value: R): Either<any, R>; const of: typeof unit; function sequenceObject<L, O extends object>(obj: { [K in keyof O]: Either<L, O[K]>; }): Either<L, O>; function sequenceArray<L, R>(array: Either<L, R>[]): Either<L, R[]>; function map<L, R, U>(f: Fun<[R], U>): Fun<[Either<L, R>], Either<L, U>>; } export {};