If need unwrap Maybe and call some function and save type without
null and undefined then use caseOf.
import { Maybe } from 'monad-maniac'
const brokenDataMaybe = Maybe.of<number>(null)
const normalDataMaybe = Maybe.of(10)
const matcher: Maybe.CaseOf<number> = {
Just: (x) => x * x,
Nothing: () => -1,
}
const unwrappedNormal = normalDataMaybe.caseOf(matcher) // 100
const unwrappedBroken = normalDataMaybe.caseOf(matcher) // -1
// More example:
brokenDataMaybe.caseOf({
Just: (x) => Maybe.of(x * x),
Nothing: () => Maybe.of(-1),
}).map((x) => x ^ 2) // Just(1)
unwrappedNormal.caseOf({
Just: (x) => Maybe.of(x * x),
Nothing: () => Maybe.of(-1),
}).map((x) => x ^ 2) // Nothing
This is object with two fields Just and Nothing what contains functions.
Works like caseOf, but use two functions as arguments for unwrap instead of object allocation.
Function will called with Just
Function will called with Nothing
Working like Maybe.map but returns not Maybe, but the function result.
If chain will call on Nothing then return undefined.
import { Maybe } from 'monad-maniac'
const result = Maybe
.of(100)
.map((x) => x / 2) // 50
.map((x) => x + 10) // 60
.chain((x) => Maybe.of(x / 3)) // 20
console.log(result) // Just(20)
const resultBad = Maybe
.of(100)
.map((x) => x / 2) // 50
.filter((x) => x > 1000) // Nothing() - next function will be called never
.map((x) => x + 10) // 60
.chain((x) => Maybe.of(x / 3)) // Nothing()
console.log(result) // Nothing()
Checking equals value from Maybe and value from other Maybe.
import { Maybe } from 'monad-maniac'
const just = Maybe.of(10)
const sameJust = Maybe.of(10)
const isEqual = equalsValue.equals(sameJust) // true
const isMoreEqual = equalsValue.equals(Maybe.of(15)) // false
Checking equals value from Maybe and what value in parameter.
import { Maybe } from 'monad-maniac'
const just = Maybe.of(10)
const isSame = equalsValue.equalsValue(10) // true
const isEqual = equalsValue.equalsValue(15) // false
Filtering value in Maybe. Takes predicate function and
return new Maybe. If the function return false, then
maybe will Noting otherwise Just.
import { Maybe } from 'monad-maniac'
const result = Maybe
.of(10)
.map((x) => x * x)
.filter((x) => x > 50) // result will Just(100)
.filter((x) => x < 90) // result will Nothing()
console.log(result.toString()) // Nothing()
Unwrap value from monad and return value from Just
or defaultValue if it was Nothing.
import { Maybe } from 'monad-maniac'
const brokenDataMaybe = Maybe.of(null)
const normalDataMaybe = Maybe.of(10)
const brokenData = brokenDataMaybe.unwrapOr('This data was broken') // This data was broken')
const normalData = Maybe.of(10).unwrapOr('This data was broken') // 10
Return true if Just
Return true if Nothing
Unwrap Maybe from Maybe, if in Maybe will not Maybe then returns Nothing.
Maybe(Maybe a) -> Maybe a
import { Maybe } from 'monad-maniac'
const just = Maybe.of(10)
const nestedJust = Maybe.of(just) // Just(Just(10))
const backJust = nestedJust.join() // Just(10)
const backMoreJust = backJust.join() // Nothing
Apply some function to value in container. map for Just
will call the function with value, for Nothing return Nothing.
If map will return null or undefined then return Nothing.
import { Maybe } from 'monad-maniac'
const zeroDivider = 0
const tenDivide = (n: number): number => n === 0 ? null : 10 / n
const foo = Maybe.of(zeroDivider).map(tenDivide) // Noting
const nonZeroDivider = 2
const bar = Maybe.of(nonZeroDivider).map(tenDivide) // Just(5)
If your Maybe contains Nothing and you want make some function
in the case you need use or. Your function will be called only
on Nothing() and result wrap in Maybe. In just your function will
be ignored.
import { Maybe } from 'monad-maniac'
const zeroDivider = 0
const tenDivide = (n: number): number => n === 0 ? null : 10 / n
// map(tenDivide) will returns `Nothing()`
const foo = Maybe.of(zeroDivider).map(tenDivide).or(() => 10) // Just(10)
Converting Maybe to Either.
import { Maybe } from 'monad-maniac'
const just = Maybe.of(10)
const nothing = Maybe.of<number>(null)
just.toEither('error') // Either.Right(10)
nothing.toEither('error') // Either.Left(error)
the value will contains in Either.Left
Returns Nothing() if Maybe is Nothing, else
Just(value converted to string).
Generated using TypeDoc
Getting
Maybe(fn)and apply function fromMaybeto value from currentMaybe