npm i opt-res
Replace null checks with the Option type, and elegantly handle optional data.
Replace exceptions with functional error handling using the Result type.
This library is heavily inspired by the excellent Option and Result enums from Rustlang.
Returns None
if the option is None
, otherwise returns optb.
Returns None
if the option is None
, otherwise calls map
with the wrapped value and returns the result.
Returns true if the option is a Some
value containing the given value. Uses strict equality.
Returns None
if the option is None
, otherwise calls predicate with the wrapped value and returns:
Some<T>
if predicate returns true (keeping the wrapped value), andNone
if predicate returns false.
This function works similar to filtering an array. You can imagine the Option<T>
being an iterator over one or zero elements. filter()
lets you decide which elements to keep.
Converts from Option<Option<T>>
to Option<T>
.
Flattening only removes one level of nesting at a time:
Returns true if the option is a None
value.
Returns true if the option is a Some
value.
Returns true if the option is a Some
and the value inside matches a predicate.
Converts the option to an array
- Array has length 1 and contains inner value if the option is a
Some
. - Array is empty if the option is a
None
value.
Maps an Option<T>
to Option<U>
by applying a function to a contained value.
Transforms the Option<T>
into a Result<T, E>
, mapping Some<T>
to Ok<T>
and None
to Err<E>
.
Returns the option if it contains a value, otherwise returns optb.
Returns the contained Some
value, consuming the self value.
Panics if the self value equals None
. Because this function may panic, its use is generally discouraged.
Returns the contained Some
value or a provided default.
Returns res if the result is Ok
, otherwise returns the Err
value of self.
Calls map
if the result is Ok
, otherwise returns the Err
value of self.
Returns true
if the result is an Ok
value containing the given value. Uses strict equals.
Returns true
if the result is an Err
value containing the given value. Uses strict equals.
Converts from Result<E, T>
to Option<E>
.
Converts from Result<E, Result<E, T>>
to Result<E, T>
.
Returns true
if the result is Err
.
Returns true
if the result is Ok
.
Converts the result to an array
- Array has length 1 and contains inner value if the result is a
Ok
. - Array is empty if the result is a
Err
value.
Maps a Result<E, T>
to Result<E, T2>
by applying a function to a contained Ok
value, leaving an Err
value untouched.
Maps a Result<E, T>
to Result<E2, T>
by applying a function to a contained Err value, leaving an Ok value untouched.
Converts from Result<E, T>
to Option<T>
.
Returns res if the result is Err
, otherwise returns the Ok
value of self.
Calls map
if the result is Err
, otherwise returns the Ok
value of self.
Calls map
on the inner value of an Ok
, or errMap
on the inner value of an Err
. map
and errMap
should produce the same output type.
Returns the contained Ok
value, consuming the self value.
Panics if the self value equals Err
. Because this function may panic, its use is generally discouraged.
Returns the contained Err
value, consuming the self value.
Panics if the self value equals Ok
. Because this function may panic, its use is generally discouraged.
Returns the contained Ok
value or a provided default.
Returns the contained Ok
value or computes it by calling map
on the inner value of the Err
.