0.3.8 • Published 6 years ago
@typed-f/either v0.3.8
@typed-f/either
Disjoint union type for Typed-F
Installation
# for NPM>=5
npm install @typed-f/either
# or
npm i @typed-f/either
# for NPM<5
npm install --save @typed-f/either
# or
npm i -S @typed-f/eitherWhy do we need Either (Disjoint union type)?
See this document.
APIs
This package includes type definition for Either type, which is an union type of Right and Left.
This package also includes their methods and some utility functions for them.
Types
Either<L, R>Right<L, R>Left<L, R>
Methods only for Either
isLeft(this: Either<L, R>): this is Left<L, R>
Checks whetherthisisLeftor not. You can use this withifstatement like following.declare const e: Either<string, number>; if (e.isLeft()) { // Now `e` is `Left<string, number>` e.value.slice(0, 1); }isRight(this: Either<L, R>): this is Right<L, R>
Counterpart ofisLeft. You can use this withifstatement too.leftOr(this: Either<L, R>, def: L): L
IfthisisLeft, this function returns the inner value ofthis. If not, this returnsdef(default value) you passed.leftOrThrow(this: Either<L, R>, err: Error): L
IfthisisLeft, this function returns the inner value ofthis. If not, this throwserryou passed.leftOrCompute(this: Either<L, R>, f: Fun<[R], L>): L
IfthisisLeft, this function returns the inner value ofthis. If not, this invokefwith inner value of currentthis, and return the result.rightOr(this: Either<L, R>, def: R): R
Counterpart ofleftOrrightOrThrow(this: Either<L, R>, err: Error): R
Counterpart ofleftOrThrowrightOrCompute(this: Either<L, R>, f: Fun<[L], R>): R
Counterpart ofleftOrCompute
Implemented Typeclasses
Most of typeclass implementation of Either considers Right as a valid value container, and Left as an error value container.
- Monad
bind<U>(this: Either<L, R>, f: Fun<[R], Either<L, U>>): Either<L, U>IfthisisRight, appliesfto its inner value and return the result. IfthisisLeft, returnsthis.
- Applicative
unit<U>(v: U): Either<any, U>ReturnRightofv.ap<U>(this: Either<L, R>, f: Either<L, Fun<[R], U>>): Either<L, U>IfthisisRightandfisRight, applies the inner value offto the inner value ofthisand returnsRightof the result. IfthisisRightbutfisLeft, returnsf. IfthisisLeft, returnsthis.
- Functor
map<U>(this: Either<L, R>, f: Fun<[R], U>): Either<L, U>IfthisisRight, appliesfto the inner value ofthisand returnsRightof the result. IfthisisLeft, returnsthis.
- Matchable
matchWith<U>(this: Either<L, R>, cases: EitherPatterns<L, R, U>): UIfthisisRight, appliescases.rightto the inner value ofthisand returns the result. IfthisisLeft, appliescases.leftto the inner value ofthisand returns the result.
- Setoid
equals(other: Either<any, any>): booleanIfthisequals toother, returntrue. Ifthisdoes not equal toother, returnfalse. When boththisandotherhave complex inner values (object), this function triesequalsmethod of inner value ofthis. In other words,this.equals(other) === this.value.equals(other.value)notEquals(other: Either<any, any>): booleanReturnstrueif and only ifthis.equals(other)returnsfalse.
Utility Functions
You can use these functions like Either.<function name>, for example, in case of map, you can access it with Either.map.
unit<R>(value: R): Either<any, R>ReturnsRightofvalue.ofAlias ofunitsequenceObject<L, O extends object>(obj: { [K in keyof O]: Either<L, O[K]> }): Either<L, O>Takes anobjofEithers and returns anEitherof object. If for all keys ofobj, correspoding values areRight, then this will return aRightof object whose keys are original keys and correspoding values are inner value of original values (Rights). If for one or more keys ofobj, values areLeft, then this will return first encountedLeftvalue. (Be careful, if there are more than oneLeftvalues inobj, returned value can be changed by JS engine codes run with)sequenceArray<L, R>(array: Either<L, R>[]): Either<L, R[]>Takes anarrayofEithers and returns anEitherof array. If all entries ofarrayareRight, this will return aRightof array whose entries are inner values of original entries. Corresponding entries will have same indices. If one or more entries ofarrayareLeft, this will return a leftmostLeftentry (whose index is minimum).map<L, R, U>(f: Fun<[R], U>): Fun<[Either<L, R>], Either<L, U>>Returns a function that takesEither<L, R>and maps its right inner value usingf.map(f)(a)is same witha.map(f).