This is alias for normal display from context (Either.Either
=> Either.Shape
)
Function like Either.attempt
This is asynctry {} catch(error) {}
for Either
.
If the function throws exception then the error will
catch and placed in Left
, and in Right
otherwise.
import { Either } from 'monad-maniac'
const errorAsyncFunction = (x: number): Promise<number> => new Promise((resolve, reject) => {
if (x === 0) {
reject(new Error('Number is zero!'))
}
resolve(x)
})
const left = await Either.asyncAttempt(errorAsyncFunction, [0])
const right = await Either.asyncAttempt(errorAsyncFunction, [10])
left.map(double).toString() // Left(Error: Number is zero!)
right.map(double).toString() // Right(20)
This is try {} catch(error) {}
for Either
.
If the function throws exception then the error will
catch and placed in Left
, and in Right
otherwise.
import { Either } from 'monad-maniac'
const errorFunction = (x: number): number => {
if (x === 0) {
throw new Error('Number is zero!')
}
return x
}
Either.attempt(errorFunction, [0]).map(double).toString() // Left(Error: Number is zero!)
Either.attempt(errorFunction, [10]).map(double).toString() // Right(20)
Method like Either.caseOf
import { Either } from 'monad-maniac'
const matcher: Either.CaseOf<string, number, number> = {
Right: (x) => x * 2,
Left: (message) => message.length,
}
const left: Either.Shape<string, number> = new Either.Left('Server error')
const right: Either.Shape<string, number> = new Either.Right(150)
Either.caseOf(matcher, left) // 12
Either.caseOf(matcher, right) // 300
Just curried caseOf
.
Method like Either.chain
import { Either } from 'monad-maniac'
const name: string | void = Math.random() > 0.5 ? 'Jake' : undefined
const result: Either.Shape<string, string> = typeof name === 'string'
? new Either.Right(name)
: new Either.Left('Server error')
const greeting = Either.chain(
(name: string) => Either.right(`Welcome, ${name}!`)
), result).get() // 'Welcome, Jake' or 'Server error'
Just curried chain
.
Method like Either.filter
Apple some function to Right
value and if the function
returns not true
then value will be placed to Left
.
import { Either } from 'monad-maniac'
const right: Either.Shape<number, number> = new Either.Right(150)
Either.filter((x) => x > 150, right) // Left(150)
Either.filter((x) => x < 300, right) // Right(150)
Just curried filter
.
Returns value
from Left
or Right
import { Either } from 'monad-maniac'
Either.fromNullable<string, number>(150).toString() // Right(150)
Either.fromNullable<string, number>(null).toString() // Left(null)
Returns value
from Left
or Right
import { Either } from 'monad-maniac'
const right = Either.right<string, number>(10)
const left = Either.left<string, number>('Some error')
Either.get(left) // 'Some error'
Either.get(right) // 10
Method like Either.getOrElse
import { Either } from 'monad-maniac'
const left = Either.left<number, number>(150)
const right = Either.right<number, number>(150)
const resultLeft = Either.getOrElse(0, left) // 0
const resultRight = Either.getOrElse(0, right) // 150
Just curried getOrElse
.
Returns true
if thisLeft
or false
otherwise.
import { Either } from 'monad-maniac'
const right = Either.right<string, number>(10)
const left = Either.left<string, number>('Some error')
Either.isLeft(left) // true
Either.isLeft(right) // false
Returns true
if thisRight
or false
otherwise.
import { Either } from 'monad-maniac'
const right = Either.right<string, number>(10)
const left = Either.left<string, number>('Some error')
Either.isRight(left) // false
Either.isRight(right) // true
Making Right
from value
import { Either } from 'monad-maniac'
const left = Either.left<string, number>('Some error')
Method like Either.map
import { Either } from 'monad-maniac'
const name: string | void = Math.random() > 0.5 ? 'Jake' : undefined
const result: Either.Shape<string, string> = typeof name === 'string'
? new Either.Right(name)
: new Either.Left('Server error')
const greeting = Either.map((name) => `Welcome, ${name}!`, result)
// ...
if (greeting.isLeft()) {
console.error(greeting.get())
} else {
showGreeting(greeting.get())
}
Just curried map
.
Alias of Either.right
Method like Either.orElse
Apple some function to Left
value.
import { Either } from 'monad-maniac'
const left: Either.Shape<string, number> = new Either.Left('Server error')
const right: Either.Shape<string, number> = new Either.Right(150)
const orElse = (message: string) => ({ message })
Either.orElse(orElse, left) // { message: 'Server error' }
Either.orElse(orElse, right) // 150
Just curried orElse
.
Making Right
from value
import { Either } from 'monad-maniac'
const right = Either.right<string, number>(10)
Method like Either.toMaybe
import { Either } from 'monad-maniac'
const left = Either.left<string, number>('error')
const right = Either.right<string, number>(144)
const nothing = Either.toMaybe(left)
const just = Either.toMaybe(right)
Returns Left
or Right
as string
.
import { Either } from 'monad-maniac'
const right = Either.right<string, number>(10)
const left = Either.left<string, number>('Some error')
Either.toString(left) // Left(Some error)
Either.toString(right) // Right(10)
Generated using TypeDoc
Mather type for caseOf