Type reprsenting some value. Library treats arbitrary value other than
Nothing
as Just
that value.
Type represent value that may not exist in which case it's Nothing
or
Just
that value.
Type representing absence of value. Library treats all of the typescript
primitives for absense of value as Nothing
.
Just provides an alias to null
.
Returns Nothing
if the left Maybe is Nothing
, otherwise returns the
right Maybe.
Maybe.and(Maybe.just(2), Maybe.nothing) // => Maybe.nothing
Maybe.and(Maybe.nothing, Maybe.just('foo')) // => Maybe.nothing
Maybe.and(Maybe.just(2), Maybe.just('foo')) // => Maybe.just('foo')
Maybe.and(Maybe.nothing, Maybe.nothing) // => Maybe.nothing
Utility to chain together two computations that may fail (return Nothing).
const makeGreeting = (name:string):string =>
`Hello ${name}!`
const greet = (name:Maybe<string>):Maybe<string> =>
Maybe.chain(makeGreeting, name)
greet(Maybe.nothing) // => Maybe.nothing
greet(Maybe.just('world')) // => Maybe.just('Hello world!')
Function that performs second computation from the a result of the first one (if it was successful - returned Just).
Maybe value, representing result of first computation.
Predicate that refines given Maybe. If returns true
given Maybe
is
refined to Just
, otherwise refined to Nothing
.
const name:Maybe<string> = Maybe.nothing
if (isJust(name)) {
console.log(name.toLowerCase())
}
Predicate that refines given Maybe. If returns true
given Maybe
is
refined to Nothing
, otherwise refined to Just
.
const name:Maybe<string> = Maybe.nothing
if (!isNothing(name)) {
console.log(name.toLowerCase())
}
Turns arbitrary value into maybe value.
arbitrary value
Transform a Maybe value with a given function:
Maybe.map(Math.sqrt, Maybe.just(9)) // => Maybe.just(3)
Maybe.map(Math.sqrt, Maybe.nothing) // => Maybe.nothing
Function that maps underlayng value.
Maybe value to be transformed.
Returns the left Maybe if it is a Just value, otherwise returns right Maybe.
Maybe.or(Maybe.just(2), Maybe.nothing) // => Maybe.just(2)
Maybe.or(Maybe.nothing, Maybe.just('foo')) // => Maybe.just('foo')
Maybe.or(Maybe.just(2), Maybe.just(100)) // => Maybe.just(2)
Maybe.or(Maybe.nothing, Maybe.nothing) // => Maybe.nothing
Provide a fallback
value, turning an optional value into a normal value.
Maybe.toValue(5, Maybe.just(9)) // => Maybe.just(9)
Maybe.toValue(6, Maybe.nothing) // => Maybe.just(6)
Fallback value
Optional value
Generated using TypeDoc
This library provides means for representing values that may or may not exist. This can come handy for representing optional arguments, error handling, and records with optional fields.