Method like Maybe.apply
but to get maybe and call method apply with a function.
import { Maybe } from 'monad-maniac'
const foo = Maybe.of(12)
const appliedFoo = Maybe.apply(Maybe.of((x) => x * x), foo) // Just(144)
Just curried apply.
Maybe(a -> b) -> Maybe(a) -> Maybe(b)
Method like Maybe.caseOf
but to get maybe and call method caseOf with a function.
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 = Maybe.caseOf(matcher, brokenDataMaybe) // 100
const unwrappedBroken = Maybe.caseOf(matcher, normalDataMaybe) // -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.
Just curried caseOf.
CaseOf a b -> Maybe(a) -> b
Method like Maybe.chain
but to get maybe and call method chain with a function.
import { Maybe } from 'monad-maniac'
const foo = Maybe.of(12)
const resultFoo = Maybe.chain((x) => x * x, foo) // 144
Just curried chain.
(a -> b) -> Maybe(a) -> b
Method like Maybe.equals
but to get maybe and call method equals with a function.
import { Maybe } from 'monad-maniac'
const firstDataMaybe = Maybe.of(10)
const secondDataMaybe = Maybe.of(10)
const isEqual = Maybe.equals(firstDataMaybe, secondDataMaybe) // true
Just curried equals.
Maybe(a) -> Maybe(a) -> boolean
Method like Maybe.equalsValue
but to get maybe and call method equalsValue with a function.
import { Maybe } from 'monad-maniac'
const just = Maybe.of(10)
const isEqual = Maybe.equalsValue(10, just) // true
Just curried equalsValue.
a -> Maybe(a) -> boolean
Unwrap Maybe with default value.
Method like Maybe.getOrElse
but to get maybe and call method getOrElse with a function.
import { Maybe } from 'monad-maniac'
const just = Maybe.of(10)
const nothing = Maybe.of<number>(null)
const normal = Maybe.getOrElse(-1, just) // 10
const bad = Maybe.getOrElse(-1, nothing) // -1
Just curried getOrElse.
(a -> b) -> Maybe(a) -> Maybe(b)
Unwrap Maybe from Maybe, if in Maybe will not Maybe then returns Nothing.
Maybe(Maybe a) -> Maybe a
Method like Maybe.join
import { Maybe } from 'monad-maniac'
const just = Maybe.of(10)
const nestedJust = Maybe.of(just) // Just(Just(10))
const backJust = Maybe.join(nestedJust) // Just(10)
const backMoreJust = Maybe.join(backJust) // Nothing
Lift function - contain function in wrapper for returning Maybe.
If function will return null or undefined, you get Nothing, else Just(some).
import { Maybe } from 'monad-maniac'
const find = <T>(list: T[]) => (predicate: (v: T) => boolean) => list.find(predicate)
type DataItem = {
id: number
name: string
}
const data: DataItem[] = [{ id: 1, name: 'Jayson' }, { id: 2, name: 'Michael' }]
const safeFindInList = Maybe.lift(find(data))
const resultJust = safeFindInList(({ id }) => id === 1) // Just([object Object])
const resultNothing = safeFindInList(({ id }) => id === 10) // Nothing()
const matcher: Maybe.CaseOf<DataItem, string> = {
Just: ({ name }) => name,
Nothing: () => 'UNKNOWN'
}
console.log(resultJust.caseOf(matcher)) // 'Jayson'
console.log(resultNothing.caseOf(matcher)) // 'UNKNOWN'
Just curried lift.
(a -> b) -> a -> Maybe(b)
Method like Maybe.map
but to get maybe and call method map with a function.
import { Maybe } from 'monad-maniac'
const foo = Maybe.of(12)
const mappedFoo = Maybe.map((x) => x * x, foo) // Just(144)
Just curried map.
(a -> b) -> Maybe(a) -> Maybe(b)
Returns Nothing()
Create a Maybe from any value.
If value will null or undefined this become Nothing()
else this be Just(value).
import { Maybe } from 'monad-maniac'
const foo = Maybe.of<string>(null) // foo will Nothing()
const bar = Maybe.of<string>('hello') // bar will Just(hello)
The type of the item contained in the Maybe.
The value to wrap in a Maybe. If it is undefined or null,
the result will be Nothing; otherwise it will be Just with value with your type.
Method like Maybe.toEither
but to get maybe and call method toEither with a left value.
import { Maybe } from 'monad-maniac'
const foo = Maybe.of(12)
const resultFoo = Maybe.map((x) => x * x, foo) // Just(144)
const eitherFoo = Maybe.toEither('Some Error', resultFoo) // Either.Right(144)
const resultBar = Maybe.map((x) => x * x, foo).filter((x) => x < 100) // Nothing()
const eitherBar = Maybe.toEither('X greater than 100', resultBar) // Either.Left(X greater than 100)
Just curried toEither.
(a -> b) -> Maybe(a) -> b
Generated using TypeDoc
Type of
caseOfmethod.Justmust be function what takes value fromMaybeif theMaybeisJust(some)and return some value.Nothingmust be function what return some value if theMaybeisNothing().