value-result
This library provides facilities for managing Result
s. A Result
is a data value representing result of a computation that may fail.
Result<error, value>
is the type used for returning and propagating errors. It is either Ok<value>
, representing success and containing a value
or an Error<error>
, representing error and containing an error
value.
Result
is a handy for representing results of computations that may fail, most prominently used for I/O.
Mapping
map
map
can be used to apply a function to a result. If the result is Ok
, it will be converted. If the result is an Error
, the same error value will propagate through.
Result // => Ok(17)Result // => Ok(17) Result // Error('Bad input')Result // Error('Bad input')
map2
Apply a function to two results, if both results are Ok
. If not, the first argument which is an Error
will propagate through.
Resultmap2 x + y Result Result // => Ok(3) Resultmap2 x + y Result Result // => Error('NaN') Resultmap2 x + y Result Result // => Error('Boom') Resultmap2 x + y Result Result // => Error('Boom')
map3
Apply a function to three results, if all results are Ok
. If not, the first result that is Error
will propagate through.
Resultmap3 a + b + c Result Result Result // => Ok(13) Resultmap3 a + b + c Result Result Result // => Error('NaN') Resultmap3 a + b + c Result Result Result // => Error('Boom') Resultmap3 a + b + c Result Result Result // => Error('Boom') Resultmap3 a + b + c Result Result Result // => Error('Oops')
map4
Apply a function to four results, if all results are Ok
. If not, the first result that is Error
will propagate through.
Resultmap4 a + b + c + d Result Result Result Result // => Ok(20) Resultmap4 a + b + c + d Result Result Result Result // => Error('Oops') Resultmap4 a + b + c + d Result Result Result Result // => Error('Oops') Resultmap4 a + b + c + d Result Result Result Result // => Error('Oops')
map5
Apply a function to five results, if all results are Ok
. If not, the first result that is Error
will propagate through.
Resultmap5 a + b + c + d + e Result Result Result Result Result // => Ok(23) Resultmap5 a + b + c + d + e Result Result Result Result Result // => Error('Oops') Resultmap5 a + b + c + d + e Result Result Result Result Result // => Error('Oops') Resultmap5 a + b + c + d + e Result Result Result Result Result // => Error('Oops')
Chaining
chain
chain
can be used to chain together a sequence of computations that may fail.
const toValidMonth = month >= 1 && month <= 12 ? Result : Result const toInt = { const int = const result = int | 0 === int ? Result : Result return result} Result // Ok(9) // => Ok(4) // => Error('Is not an Integer') // => Error('months must be between 1 and 12')
Handling Errors
withDefault
withDefault
can be used to extract the potential Ok
value from the Result
. But since Result
maybe be an Error
you need to provide a default value
to be returned instead for such cases:
Result // => 123 // => 0
format
Format the error value of a result. If the result is Ok, it stays exactly the same, but if the result is an Err we will format the error. For example, say the errors we get have too much information:
format
format
lets format the error value of a result. If the result is Ok, it stays exactly the same, but if the result is an Error it will format the error. For example, say the errors we get have too much information:
const readInt = { const match = input; const result = match == null ? Result : Result return result} const toMessage = errormessage; Result // => Ok(123) // => Ok(1234) // => Error('Character "a" is not a number')
capture
capture
can be used to control flow based on result values:
const square = Resultconst fail = Result Result // => Ok(2) Result // => Ok(2) Result // => Ok(9) Result // => Error(-17)
Other
and
Returns right
result if the left
result is Ok
, otherwise returns the Error
value of the left
.
const two = Resultconst late = Result two // => Error('Late error')Result // => Error('Late error') const early = Result early // => Error('Early error')Result // => Error('Early error') early // => Error('Early error')Result // => Error('Early error') const other = Result two // => Ok("Another")Result // => Ok("Another")
or
Returns right
if the result is Error
, otherwise returns the Ok
value of the left
.
const two = Resultconst late = Result two // => Ok(2)Result // => Ok(2) const early = Result early // => Ok(2)Result // => Ok(2) early // => Error('Late error')Result // => Error('Late error') const other = Resulttwo // => Ok(2)Result // => Ok(2)
Install
npm install value-result