@typed-f/either
v0.3.8
Published
[![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
Downloads
40
Maintainers
Readme
@typed-f/either
repo-circleci-badge: https://img.shields.io/circleci/project/github/Ailrun/typed-f/master.svg?logo=circleci
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/either
Why 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 whetherthis
isLeft
or not. You can use this withif
statement 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 withif
statement too.leftOr(this: Either<L, R>, def: L): L
Ifthis
isLeft
, this function returns the inner value ofthis
. If not, this returnsdef
(default value) you passed.leftOrThrow(this: Either<L, R>, err: Error): L
Ifthis
isLeft
, this function returns the inner value ofthis
. If not, this throwserr
you passed.leftOrCompute(this: Either<L, R>, f: Fun<[R], L>): L
Ifthis
isLeft
, this function returns the inner value ofthis
. If not, this invokef
with inner value of currentthis
, and return the result.rightOr(this: Either<L, R>, def: R): R
Counterpart ofleftOr
rightOrThrow(this: Either<L, R>, err: Error): R
Counterpart ofleftOrThrow
rightOrCompute(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>
Ifthis
isRight
, appliesf
to its inner value and return the result. Ifthis
isLeft
, returnsthis
.
- Applicative
unit<U>(v: U): Either<any, U>
ReturnRight
ofv
.ap<U>(this: Either<L, R>, f: Either<L, Fun<[R], U>>): Either<L, U>
Ifthis
isRight
andf
isRight
, applies the inner value off
to the inner value ofthis
and returnsRight
of the result. Ifthis
isRight
butf
isLeft
, returnsf
. Ifthis
isLeft
, returnsthis
.
- Functor
map<U>(this: Either<L, R>, f: Fun<[R], U>): Either<L, U>
Ifthis
isRight
, appliesf
to the inner value ofthis
and returnsRight
of the result. Ifthis
isLeft
, returnsthis
.
- Matchable
matchWith<U>(this: Either<L, R>, cases: EitherPatterns<L, R, U>): U
Ifthis
isRight
, appliescases.right
to the inner value ofthis
and returns the result. Ifthis
isLeft
, appliescases.left
to the inner value ofthis
and returns the result.
- Setoid
equals(other: Either<any, any>): boolean
Ifthis
equals toother
, returntrue
. Ifthis
does not equal toother
, returnfalse
. When boththis
andother
have complex inner values (object), this function triesequals
method of inner value ofthis
. In other words,this.equals(other) === this.value.equals(other.value)
notEquals(other: Either<any, any>): boolean
Returnstrue
if 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>
ReturnsRight
ofvalue
.of
Alias ofunit
sequenceObject<L, O extends object>(obj: { [K in keyof O]: Either<L, O[K]> }): Either<L, O>
Takes anobj
ofEither
s and returns anEither
of object. If for all keys ofobj
, correspoding values areRight
, then this will return aRight
of object whose keys are original keys and correspoding values are inner value of original values (Right
s). If for one or more keys ofobj
, values areLeft
, then this will return first encountedLeft
value. (Be careful, if there are more than oneLeft
values inobj
, returned value can be changed by JS engine codes run with)sequenceArray<L, R>(array: Either<L, R>[]): Either<L, R[]>
Takes anarray
ofEither
s and returns anEither
of array. If all entries ofarray
areRight
, this will return aRight
of array whose entries are inner values of original entries. Corresponding entries will have same indices. If one or more entries ofarray
areLeft
, this will return a leftmostLeft
entry (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)
.