https://github.com/zerobias/apropos
Fast strong typed 'Either' data structure for typescript and flow
https://github.com/zerobias/apropos
adt either fp functional maybe monad
Last synced: 7 months ago
JSON representation
Fast strong typed 'Either' data structure for typescript and flow
- Host: GitHub
- URL: https://github.com/zerobias/apropos
- Owner: zerobias
- License: mit
- Created: 2017-08-11T04:01:23.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-12-11T05:58:51.000Z (almost 8 years ago)
- Last Synced: 2025-03-17T20:08:15.116Z (7 months ago)
- Topics: adt, either, fp, functional, maybe, monad
- Language: JavaScript
- Size: 266 KB
- Stars: 20
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Apropos
Fast strong typed 'Either' data structure for typescript and flow
```bash
$ npm install --save apropos
```[![npm version][npm-image]][npm-url]
[](https://travis-ci.org/zerobias/apropos)## API
- [of](#of)
- [ofL](#ofl)
- [Right](#right)
- [Left](#left)
- [is](#is)
- [makeError](#makeerror)```javascript
//@flowimport defaultOf, {
of,
ofL,
Right,
Left,
is,
makeError,
type Apropos,
type MakeError,
} from 'apropos'
```### of
```javascript
function of(value: R): Apropos
```Create pure right-handed value, left-handed type is empty.
Exports by default### ofL
```javascript
function ofL(value: L): Apropos
```Create pure left-handed value, right-handed type is empty.
### Right
```javascript
function Right<-L, R>(value: R): Apropos
```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
```javascript
function Left(value: L): Apropos
```Create left-handed value, right-handed type is inferred from usage
### is
```javascript
function is<-T>(value: T): boolean
```Checks whether an object is an instance of `Apropos`
### makeError
```javascript
class AnnotatedError extends Error {
tag: Tag
data: Context
}function makeError<-Tag>(tag: Tag): (data: Context) => AnnotatedError
```Create fabric for generating tagged error constructors.
Useful in `.mapL`.See [annotated errors](#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
```typescript
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](./LICENSE)
[npm-url]: https://www.npmjs.org/package/apropos
[npm-image]: https://badge.fury.io/js/apropos.svg