apropos
v0.6.2
Published
Strong typed functional 'Either', 'Maybe', 'Identity' and 'Tuple'
Downloads
31
Maintainers
Readme
Apropos
Fast strong typed 'Either' data structure for typescript and flow
$ npm install --save apropos
API
//@flow
import defaultOf, {
of,
ofL,
Right,
Left,
is,
makeError,
type Apropos,
type MakeError,
} from 'apropos'
of
function of<R>(value: R): Apropos<void, R>
Create pure right-handed value, left-handed type is empty. Exports by default
ofL
function ofL<L>(value: L): Apropos<L, void>
Create pure left-handed value, right-handed type is empty.
Right
function Right<-L, R>(value: R): Apropos<L, R>
Create right-handed value, left-handed type is inferred from usage.
Technically, Right
returns the same as of
; the difference is only in the type inference.
Left
function Left<L, -R>(value: L): Apropos<L, R>
Create left-handed value, right-handed type is inferred from usage
is
function is<-T>(value: T): boolean
Checks whether an object is an instance of Apropos
makeError
class AnnotatedError<Context, Tag> extends Error {
tag: Tag
data: Context
}
function makeError<-Tag>(tag: Tag): <Context>(data: Context) => AnnotatedError<Context, Tag>
Create fabric for generating tagged error constructors.
Useful in .mapL
.
See annotated errors
Instance methods
isRight
isLeft
equals
thru
orElse
swap
promise
fold
Maps
map
mapR
mapL
bimap
Taps
tap
tapR
tapL
bitap
Chains
chain
chainR
chainL
bichain
Conditions
cond
chainCond
logic
Combinations
alt
and
ap
Annotated errors
import { of, makeError, MakeError, Left } from 'apropos'
const notNumber: MakeError<'Not a number'> = makeError('Not a number')
const isNegative: MakeError<'Negative number'> = makeError('Negative number')
const positiveNum =
of(-2)
.map(x => x + 1)
.chain(x => typeof x === 'number'
? of(x)
: Left(x))
.mapL(notNumber)
.logic({
cond: x => x > 0,
pass: x => 'Positive number: ' + x,
fail: isNegative
})
positiveNum.fold(x => console.log(x), x => console.error(x))
// => Annotated Error: 'Negative number' -1
The project is released under the Mit License