import { type IResult } from "https://deno.land/x/eitherway@0.10.0/lib/core/result.ts";
Base interface implemented by Ok<T>
and Err<E>
Properties
This well-known symbol is called by Object.prototype.toString
to
obtain a string representation of a value's type
This maybe useful for debugging or certain logs
The [.toString()
]this#toString method is a useful short-hand in these scenarios
See the reference
Methods
Use this to return the Result itself.
Canonical identity function. Mainly useful for flattening instances of
Result<Result<T, E>, E>
in combination with .andThen()
Use this to obtain a deep clone of Result<T, E>
Under the hood, this uses the structuredClone
algorithm exposed via
the global function of the same name
May incur performance penalties, depending on the platform, size and type of the data to be cloned
Can be handy if user-defined operations on reference types mutate the passed value and the original value should be retained
CAUTION: Mutations in a chained series of operations are strongly discouraged
See the reference
Use this to map the encapsulated value <T>' to
`.
In case of Err<E>
this method short-circuits. See IResult#mapErr
for the opposite case.
Same as .map()
but in case of Err<E>, a new instance of
Ok, wrapping the provided
orValue` will be returned.
Same as .map()
but in case of Err<E>, a new instance of
Ok, wrapping the return value of the provided
elseFn` will be returned.
Use this if the fallback value is expensive to produce.
Use this to map the encapsulated value <E>
to <E2>
. In case of
Ok<T>
, this method short-circuits. See IResult#map
for the opposite case.
Use this to produce a new instance of Result<T2, E2>
from the
encapsulated value <T>
. Can be used to flatten an instance of
Result<Result<T2, E2>, E>
to Result<T2, E | E2>
.
In case of Err<E>
, this method short-circuits.
See IResult#orElse
for the opposite case.
Canonical .flatMap()
or .chain()
method.
Use this to produce a new instance of Result<T2, E2>
from the
encapsulated value <E>
. Can be used to flatten an instance of
Result<T, Result<T2, E2>>
to Result<T | T2, E2>
.
In case of Ok<T>
, this method short-circuits.
See IResult#andThen
for the opposite case.
Use this to conditionally pass-through the encapsulated value <T>
based upon the outcome of the supplied ensureFn
.
In case of Err<E>
, this method short-circuits.
In case of Ok<T>
, the supplied ensureFn
is called with the encapsulated
value <T>
and if the return value is:
Ok<T2>
: it is discarded and the originalOk<T>
is returnedErr<E2>
:Err<E2>
is returned
See IResult#orEnsure
for the opposite case.
This is equivalent to chaining:
original.andThen(ensureFn).and(original)
LHS andEnsure RHS | RHS: Ok | RHS: Err |
---|---|---|
LHS: Ok | Ok | Err |
LHS: Err | Err | Err |
Use this to conditionally pass-through the encapsulated value <E>
based upon the outcome of the supplied ensureFn
.
In case of Ok<T>
, this method short-circuits.
In case of Err<E>
, the supplied ensureFn
is called with the encapsulated
value <E>
and if the return value is:
Ok<T2>
: it is returnedErr<T2>
: it is discarded and the originalErr<E>
is returned
See IResult#andEnsure
for the opposite case.
This is equivalent to chaining:
original.orElse(ensureFn).or(original)
LHS orEnsure RHS | RHS: Ok | RHS: Err |
---|---|---|
LHS: Ok | Ok | Ok |
LHS: Err | Ok | Err |
Logical AND (&&
)
Returns RHS if LHS is Ok
, otherwise returns LHS
LHS AND RHS | RHS: Ok | RHS: Err |
---|---|---|
LHS: Ok | Ok | Err |
LHS: Err | Err | Err |
Logical OR (||
)
Returns RHS if LHS is Err
, otherwise returns LHS
|LHS || RHS |RHS: Ok|RHS: Err| |:------------: |:-------------:|:--------------:| |LHS: Ok | Ok | Ok | |LHS: Err| Ok | Err |
Use this to zip the encapsulated values of two Ok
instances into
a new Ok
instance wrapping a tuple of them.
In case any of the two Result
instances is Err
, the respective
Err
instance is returned in left-to-right evaluation order.
LHS zip RHS | RHS: Ok | RHS: Err |
---|---|---|
LHS: Ok | Ok<[T, T2]> | Err |
LHS: Err | Err | Err |
This is not only useful to produce tuples, but also to collect arguments to be passed to a function down the line.
Use this to get the wrapped value out of an Result<T, E>
instance
Returns the wrapped value of type <T>
in case of Ok<T>
OR
<E>
in case of Err<E>
.
It is necessary to narrow the instance to Ok<T>
or Err<E>
in
order to narrow the return value.
In contrast to other implementations, this method NEVER throws an exception
Same as .unwrap()
but returns a default value in case of Err<E>
Same as .unwrap()
but returns a the result of the provided elseFn
in
case of Err<E>
Use this if the fallback value is expensive to produce
Use this to cast an instance of Ok<T>
or Err<E>
to it's Result
representation. This is a safe, inherent type cast. Does not alter
the runtime behaviour.
Mostly useful in the context of early returns or combination methods,
where an already narrowed instance would result in one of the variants
being inferred as unknown
otherwise.
Use this to obtain a tuple representation of Result<T, E>
Use this to transform an instance of Result<T, E>
into an Option<T>
.
See IResult#err
for the opposite case.
Use this to transform an instance of Result<T, E>
into an Option<E>
.
See IResult#ok
for the opposite case.
Use this to transform an instance of Result<T, E>
into a type of your
choosing.
This is mostly useful to push the instance into an async context.
Use this to obtain an iterator over the wrapped value <T>
in case of Ok
In case of Err
, an empty iterator is returned
Use this to perform side-effects transparently.
The tapFn
receives a deep clone of Result<T, E>
IResult#clone
This may have performance implications, dependending on the size of
the wrapped value <T | E>
, but ensures that the tapFn
can never
change or invalidate the state of the Result<T, E>
instance
See the reference
Use this to inspect the the encapsulated value <T>
transparently.
Mainly used for debugging and logging purposes.
In case of Err<E>
, this method short-cuits and returns the Err<E>
instance. See IResult#inspectErr
for the opposite case;
Use this to inspect the the encapsulated value <E>
transparently.
Mainly used for debugging and logging purposes.
In case of Ok<T>
, this method short-cuits and returns the Ok<T>
instance. See IResult#inspect
for the opposite case;
Use this to get the full string tag
Short-hand for Object.prototype.toString.call(result)
See the reference
Delegates to the implementation of the wrapped value <T>
or exhausts
the iterator by returning { done: true, value: undefined }
if <T>
doesn't
implement the iterator protocol
Err
represents the empty iterator and returns the empty iterator result
{ done: true, value: undefined }
See the reference