Space-monad
Option
and Result
monads for TypeScript.
Option
- Option()
- Option.all()
- Option.isOption
- None
- map
- flatMap
- filter
- fold
- orElse
- isDefined
- get
- getOrElse
- forEach
- contains
- exists
- toArray
Creating an Option
Option(x)
Creates an Option from a value. If the value is null or undefined, it will create a None, else a Some.
If you already know the value is defined for sure (not nullable) or not, you can create a Some
or None
directly:
Option.all([...optionsOrValues])
Creates a new Option holding the tuple of all the values contained in the passed array if they were all Some or non null/undefined values, else returns None
// some === Some([10, 20, 5]) // none === None
Option.isOption
Returns whether the passed instance in an Option, and refines its type
Option.isOptionSome33 // true
None
The Option constant representing no value.
Transforming an Option
map
Maps the value contained in this Some, else returns None. Depending on the map function return value, a Some could be tranformed into a None, as a Some is guaranteed to never contain a null or undefined value.
// some === Some(66)
flatMap
Maps the value contained in this Some to a new Option, else returns None.
// some === Some(44)
filter
If this Option is a Some and the predicate returns true, keep that Some. In all other cases, return None.
// some === Some(33)
fold
Applies the first function if this is a None, else applies the second function. Note: Since this method creates 2 functions everytime it runs, don't use in tight loops; use isDefined() instead.
toArray
Transforms this option into an Array or either 1 or 0 element.
orElse
Returns this Option unless it's a None, in which case the provided alternative is returned.
// some === Some(33)
Misc
get
Some
instances return their value, whereas None
always return undefined
.
This method never throws.
// value === 33
isDefined
Returns whether this Option has a defined value (i.e, it's a Some(value)) Note: this refines the type of the Option to be a Some so it's guaranteed its value is not null/undefined.
getOrElse
Returns this Option's value if it's a Some, else return the provided alternative
// value === 33
forEach
Applies the given procedure to the option's value, if it is non empty.
Option33.forEachconsole.logx
contains
Returns whether this option is a Some that contain a specific value, using ===
Option30.contains30 // true
exists
Returns whether this option is a Some with a value satisfying the predicate.
Option30.existsn > 10 // true
Result
A Result
is the result of a computation that may fail. An Ok
represents a successful computation, while an Err
represent the error case.
Importing Result
Here's everything that can be imported to use Results:
Result.isResult
Returns whether this instance is a Result (either an Ok or a Err) and refines its type
Result.isResultOk10 // true
Result.all
Creates a new Ok Result holding the tuple of all the values contained in the passed array if they were all Ok, else returns the first encountered Err.
isOk
Returns whether this is an instance of Ok
Ok10.isOk // true
map
Maps the value contained in this Result if it's an Ok, else propagates the Error.
Ok10.mapx * 2 // Ok(20)Err10.mapx * 2 // Err(10)
mapError
Maps the Error contained in this Result if it's an Err, else propagates the Ok.
Ok10.mapErrorx * 2 // Ok(10)Err10.mapErrorx * 2 // Err(20)
flatMap
Maps the value contained in this Result with another Result if it's an Ok, else propagates the Error. Note: It is allowed to return a Result with a different Error type.
Ok10.flatMapOkx * 2 // Ok(20)Ok10.flatMapErrx * 2 // Err(20)
fold
Applies the first function if this is an Err, else applies the second function. Note: Don't use in tight loops; use isOk() instead.
Ok10.fold console.errorerr, num * 2 // 20