Handling possible errors or values in a functional way.
Heavily inspired by rust std::option and std::result
# npm
npm i tiinvo
# yarn
yarn add tiinvo
Type Option
represents an optional value: every Option
is either Some
and contains a value, or None
, and does not.
import { Option, Some, None } from 'tiinvo';
Some('foo'); // this is some
None() // this is none
Some('foo').and(Some('bar')).and(Some('baz')).unwrap() // 'baz'
Some('foo').and(None()).and(Some('baz')).isSome() // false
Option(null).isSome() // false
Option(100).isSome() // true
By default, both null
, NaN
and undefined
are considered None
types.
Returns None
if the option is None
, otherwise returns optb
import { Option } from 'tiinvo';
Option(10).and(Option(20)).isSome() // true
Option(null).and(Option(20)).isSome() // false
Returns callback
result if OptionLike<T>
is Some
, otherwise returns None
import { Option } from 'tiinvo';
Option(10).andThen(value => Option(value * 2)) // Option(20)
None().andThen(value => Option(value * 2)) // None()
Throws if the value is a None
with a custom error message provided by msg.
import { Option } from 'tiinvo';
Option(1).expect('ok') // Option(1)
Option(null).expect('myerror') // throws ReferenceError('myerror')
Returns the Option
if it's value passes the predicate
function. Otherwise returns None
Option(1).filter(a => a > 0).isSome() // true
Option(1).filter(a => a > 10).isSome() // false
Converts from Option<Option<T>>
to Option<T>
Option(Some(100)).flattern() // Some(100)
Returns if has not a value
None().isNone() // true
None().isSome() // false
Returns if has a value
Some().isSome() // true
Some().isNone() // false
Maps an OptionLike<T>
to OptionLike<U>
by applying a function to a contained value.
Option('foobar').map(val => val.length) // Option(6)
Applies a function to the contained value (if any), or returns the provided default (if not).
Option('foobar').mapOr('abc', arg => arg.length) // Option(6)
None().mapOr('abc', arg => arg.length) // Option('abc')
Applies a function to the contained value (if any), or computes a default (if not).
Option('helloworld').mapOrElse(() => 0, arg => arg.length) // 10
None().mapOrElse(() => 1000, arg => arg.length) // 1000
Transforms the OptionLike<T>
into a Result<T, E>
, mapping Some(v)
to Ok(v)
and None
to Err(err)
.
Some(100).okOr(Err('foo')) // Ok(100)
None().okOr(Err('foo')) // Err('foo')
Transforms the OptionLike<T>
into a Result<T, E>
, mapping Some(v)
to Ok(v)
and None
to Err(err())
.
Some(100).okOrElse(() => Err("foo")); // Ok(100)
None().okOrElse(() => Err("foo")); // Err('foo)
Returns the option if it contains a value, otherwise returns optb
None().or(Some(100)) // Some(100)
Some(10).or(Some(100)) // Some(10)
Returns the option if it contains a value, otherwise calls f
and returns the result.
Some(10).orElse(() => 1000) // Some(10)
None().orElse(() => 1000) // Some(1000)
Transposes an Option
of a Result
into a Result
of an Option
.
Option(100).transpose() // Ok(Some(100))
Returns Some if exactly one of self, optb is Some, otherwise returns None.
Some(10).xor(Some(10)) // None()
Some(10).xor(Some(11)) // Some(10)
None().xor(Some(11)) // Some(11)
None().xor(None()) // None()
Returns wrapped value or throws if is None
Some(10).unwrap() // 10
None().unwrap() // throw ReferenceError
Returns the contained value or a default.
None().unwrapOr(10) // 10
Some(20).unwrapOr(10) // 20
Result<T, E>
is the type used for returning and propagating errors.
It is an enum with the variants, Ok(T)
, representing success and containing a value, and Err(E)
, representing error and containing an error value.
import { Ok, Err } from 'tiinvo';
Ok(200)
Err('this is an error')
Returns res
if the result is Ok
, otherwise returns the Err
value of self.
Ok(10).and(Ok(20)) // Ok(20)
Err('meh').and(Ok(30)) // Err('meh')
Calls op
if the result is Ok
, otherwise returns the Err
value of self.
Ok(20).andThen(a => a * 2) // Ok(40)
Err('aaa').andThen(a => a * 2) // Err('aaa')
Converts from Result<T, E>
to OptionLike<E>
.
Err('foo').err() // OptionLike(new Error('foo'))
Unwraps a result, yielding the content of an Ok
.
Ok(10).expect('does not explode') // 10
Err('argh').expect('will explode') // throws Error('will explode')