1.0.3 • Published 1 year ago
@nidstang/result v1.0.3
Usage
https://elrincondelfront.substack.com/p/manejo-de-errores-con-javascript
API
type Result<T,E> = {
/**
* returns true if Result is an ok and false otherwise
*/
isOk() : Boolean,
/**
* returns true if Result is a fail and false otherwise
*/
isFail() : Boolean,
/**
* returns true if the value provided is the same as the result value
*/
containsValue(value : T) : Boolean,
/**
* apply the provided function over the value when Result is an ok. Returns the same result otherwise
*/
map(fn: (value : T) => T) : Result<T,E>,
/**
* apply the provided function that returns a Result when Result is an ok. Returns the same result otherwise
*/
flatMap(fn: (value : T) => Result<T,E>) : Result<T,E>,
/**
* goes to a promise. resolve if result is ok and reject otherwise
*/
resolve() : Promise<T,E>,
/**
* same as map but only when result is a fail. Returns same result otherwise
*/
mapErr(fn : (error : E) => Resutl<T,E>) : Result<T,E>,
/**
* map a function over a ok value and if it's a fail it will return the default value
*/
mapOr<K>(defaultValue : K, fn: (value : T) => T) : Result<T,E> | K,
/**
* same as mapOr but lazily executed. rather than a defaultvalue, a default function is return
*/
mapOrElse<K>(defaultFn : (value : T) => K, fn: (value : T) => T) : Result<T,E> | K,
/**
* allows to break the result. if it's ok the T value is return, otherwise default value is returned
*/
unwrapOr<K>(defaultValue : K) : T | K,
}