Works like caseOf, but use two functions as arguments for unwrap instead of object allocation.
Function will called with Left
Function will called with Right
Apply some function to value in container. map
for Right
will call the function with value, for Left
return Left
else value what be returned from the function.
import { Either } from 'monad-maniac'
const divide = (dividend: number) => (divider: number): Either.Shape<string, number> => {
if (divider === 0) {
return Either.left('Divider is zero!')
}
return Either.right(dividend / divider)
}
const nonZeroMultiply = (multiplicand: number) => (factor: number): Either.Shape<string, number> => {
if (factor === 0) {
return Either.left('Factor is zero!')
}
return Either.right(multiplicand * factor)
}
const resultNormal = divide(10)(2).chain(nonZeroMultiply(20)).get() // 100
const resultErrorDivide = divide(10)(0).chain(nonZeroMultiply(20)).get() // 'Divider is zero!'
const resultErrorMultiply= divide(0)(2).chain(nonZeroMultiply(20)).get() // 'Factor is zero!'
const resultError = divide(0)(0).chain(nonZeroMultiply(20)).get() // 'Divider is zero!'
Function to apply for Right value
Apply predicate function to value in container.
If the function returns not true
then value from
Right
will be transferred to Left
.
import { Either } from 'monad-maniac'
const example = Either.right<number, number>(0)
const result = example.filter((x) => x !== 0).map((x) => 1 / x).get() // 0
Just returns value
from Right
or Left
import { Either } from 'monad-maniac'
const left = Either.left<number, number>(300)
const right = Either.right<number, number>(150)
const resultLeft = left.get() // 300
const resultRight = right.get() // 150
If Either
is Left
returns defaultValue
and returns value
from Right
otherwise.
import { Either } from 'monad-maniac'
const left = Either.left<number, number>(150)
const right = Either.right<number, number>(150)
const resultLeft = left.getOrElse(0) // 0
const resultRight = right.getOrElse(0) // 150
Returns true
if it's Left
and false
otherwise.
import { Either } from 'monad-maniac'
const left = Either.left<number, number>(300)
const right = Either.right<number, number>(150)
const resultLeft = left.isLeft() // true
const resultRight = right.isLeft() // false
Returns false
if it's Left
and true
otherwise.
import { Either } from 'monad-maniac'
const left = Either.left<number, number>(300)
const right = Either.right<number, number>(150)
const resultLeft = left.isRight() // false
const resultRight = right.isRight() // true
Apply some function to value in container. map
for Right
will call the function with value, for Left
return Left
.
If map
will return null
or undefined
then return Right
with that.
Be careful.
import { Either } from 'monad-maniac'
const divide = (dividend: number) => (divider: number): Either.Shape<string, number> => {
if (divider === 0) {
return Either.left('Divider is zero!')
}
return Either.right(dividend / divider)
}
const resultNormal = divide(10)(5).map(double).get() // 4
const resultErrorDivide = divide(10)(0).map(double).get() // 'Divider is zero!'
Function to apply for Right value
Apply some function to value in container. map
for Left
.
Returns value will be wrap in Right
import { Either } from 'monad-maniac'
const divide = (dividend: number) => (divider: number): Either.Shape<string, number> => {
if (divider === 0) {
return Either.left('Divider is zero!')
}
return Either.right(dividend / divider)
}
const resultErrorDivide = divide(10)(0).map(double).get() // 'Divider is zero!'
const resultErrorDivide = divide(10)(0).map(double).or(() => 0).get() // 0
Function to apply for Right value
Apply some function to value in container Left
. Like map
for Right
.
The methods returns result from function or value from Right
.
import { Either } from 'monad-maniac'
const left = Either.left<Error, string>(new Error('Some error'))
const right = Either.right<Error, string>('Jake')
const resultLeft = left.orElse((error) => error.message) // Some error
const resultRight = right.orElse((error) => error.message) // Jake
Function to apply for Right value
Converting Either
to Maybe
.
If value in Left
will returns Nothing
, else
returns result Maybe.of
for value from Right
.
import { Either } from 'monad-maniac'
const left = Either.left<string, number>('Some Error')
const right = Either.right<string, number>(150)
const rightVoid = Either.right<string, number | void>(undefined)
const resultLeft = left.toMaybe() // Nothing()
const resultRight = right.toMaybe() // Just(150)
const resultRightVoid = rightVoid.toMaybe() // Nothing()
Returns Left
or Right
as string
: Left(left value)
or Right(right value)
.
import { Either } from 'monad-maniac'
const left = Either.left<number, number>(300)
const right = Either.right<number, number>(150)
const resultLeft = left.toString() // Left(300)
const resultRight = right.toString() // Right(150)
Generated using TypeDoc
Returns
false
if it'sLeft
andtrue
otherwise.