@jdeurt/maybe
An implementation of the Maybe monad in Typescript.
Installation
To install, run:
yarn add @jdeurt/maybe
API Documentation
Maybe<A>
Class: Maybe<A>
is an abstract class that represents a type that may either hold a value of type A
(represented as Just<A>
) or not (represented as Nothing<A>
).
of
Static Method: Creates a new Maybe<A>
instance.
nothing
Static Method: Creates a new Nothing<A>
instance.
just
Static Method: Creates a new Just<A>
instance.
isNothing
Method: Returns true
if the Maybe<A>
instance is Nothing<A>
, false
otherwise.
isJust
Method: Returns true
if the Maybe<A>
instance is Just<A>
, false
otherwise.
map
Method: Transforms the value in the Just<A>
using the function, if the Maybe<A>
is Just<A>
. If the Maybe<A>
is Nothing<A>
, does nothing.
flatMap
Method: Similar to map
, but the provided function should return a Maybe<B>
instance.
apply
Method: Applies the function contained in the Maybe<A>
to another Maybe<A>
, if both Maybe<A>
instances are Just<A>
. If either Maybe<A>
is Nothing<A>
, does nothing.
equals
Method: Checks if two Maybe<A>
instances are equal.
getOrElse
Method: If the Maybe<A>
instance is Just<A>
, returns its value, otherwise returns the default value.
orElse
Method: If the Maybe<A>
instance is Just<A>
, returns it, otherwise returns Just<A>
of the default value.
get
Method: Returns the value of the Maybe<A>
instance if it's Just<A>
, otherwise throws an error.
toString
Method: Returns the string representation of the Maybe<A>
instance.
Nothing<A>
Class: Nothing<A>
is a class that represents the absence of a value. It extends Maybe<A>
.
of
Static Method: Creates a new Nothing<A>
instance.
Just<A>
Class: Just<A>
is a class that represents the presence of a value. It extends Maybe<A>
.
of
Static Method: Creates a new Just<A>
instance.