the expected error type
the expected value type
if both argument are Success
return a tuple containing both values wrapped in a Success
if not, the leftmost argument with the highest priority as defined bellow will be returned
Failure
being the highest priority, Loading
being the lowest
Failure -> NotAsked -> Loading
import { append, failure, success } from 'dataway';
append(success('a'), success('b')) === success(['a', 'b']);
append(failure('a'), success('b')) === failure('a');
append(failure('a'), failure('b')) === failure('a');
Fold is the only way to attempt to safely extract the data, because it force you to consider the four variations of the dataway state. The following example demonstrate how useful this is when trying to render a search result. We may want to notify the user why no result did show up on his screen
import { dataway, fold } from "dataway";
const searchResult = dataway.of(["result1", "result2", "result3", "result4"]);
fold(
() => "<span>use the Search button for nice code snippet</span>",
() => "<span>Search initiated, awesome code snippets incomings !</span>",
error => `<span>ho no ! ${error} did happen, please retry</span>`,
results => `<ul>${results.map(result => `<li>${result}</li>`)}</ul>`,
searchResult
);
the error type you expect
the success type you expect
the type of value that you want to produce
Constructs a new Dataway
from a fp-ts
Either
type.
If the value is a Left value
it returns Failure value
otherwise it returns the Right
value wrapped in a Success
parameter | Result |
---|---|
Left error | Failure error |
Right foo | Success foo |
import { failure, success, fromEither } from 'dataway';
import { left, right } from 'fp-ts/lib/Either';
fromEither(left(foo)) === failure(foo);
fromEither(right(bar)) === success(bar);
type of the provided Left wrapped value
type of the provided Right wrapped value
Constructs a new Dataway
from a nullable type.
If the value is null
or undefined
it returns NotAsked
,
otherwise it returns the value wrapped in a Success
parameter | Result |
---|---|
null | NotAsked |
undefined | NotAsked |
foo | Success foo |
import { notAsked, success, fromNullable } from 'dataway';
fromNullable(null) === notAsked;
fromNullable('foo') === success('foo');
type of the provided value
Constructs a new Dataway
from a fp-ts
Option
type.
If the value is a None
it returns NotAsked
otherwise it returns the Some
value wrapped in a Success
parameter | Result |
---|---|
None | NotAsked |
Some foo | Success foo |
import { failure, success, fromEither } from 'dataway';
import { none, some } from 'fp-ts/lib/Option';
fromEither(none) === notAsked;
fromEither(some(foo)) === success(foo);
Compare two dataway
import { getEq } from "dataway";
const E = getEq(eqNumber, eqString);
E.equals(notAsked, notAsked) // true
E.equals(loading, loading) // true
E.equals(failure(1), failure(2)) // false
E.equals(failure(1), failure(1)) // true
E.equals(success('hello'), success('olleh')) // false
E.equals(success('hello'), success('hello')) // true
Eq instance to compare failure values
Eq instance to compare success values
apply the function f
if both arguments are Success
if not, the leftmost argument with the highest priority as defined bellow will be returned
Failure
being the highest priority, Loading
being the lowest
Failure -> NotAsked -> Loading
import { map2, failure, success } from 'dataway';
const add = val1 => val2 => val1 + val2;
map2(add, success(3), success(5)) === success(8);
map2(add, failure('a'), success(5)) === failure('a');
map2(add, failure('a'), failure('b')) === failure('a');
apply the function f
if all arguments are Success
if not, the leftmost argument with the highest priority as defined bellow will be returned
Failure
being the highest priority, Loading
being the lowest
Failure -> NotAsked -> Loading
import { map3, failure, success } from 'dataway';
const cubic = val1 => val2 => val3 => val1 * val2 * val3;
map3(cubic, success(3), success(5), success(2)) === success(16);
map3(cubic, failure('a'), success(5), loading) === failure('a');
map3(cubic, success(3), loading, notAsked) === notAsked;
apply MonadA value to MonadAtoB function if both are Success to procude a MonadB
following table illustrate the dataway ap combinations
Monad(a -> b) | Monad(a) | Result |
---|---|---|
success(a -> b) | succes(a) | success(b) |
success(a -> b) | notAsked | notAsked |
success(a -> b) | loading | loading |
success(a -> b) | failure(c) | failure(c) |
notasked | failure(c) | failure(c) |
loading | failure(c) | failure(c) |
failure(d) | failure(c) | failure(d) |
Allow to turn function a -> Dataway b
into a Dataway a -> Dataway b
Where Dataway b
can use a different constructor than Dataway a
f(a -> Dataway b) | Monad(a) | Result |
---|---|---|
f(a -> Success b) | success(a) | success(b) |
f(a -> Failure b) | success(a) | failure(b) |
f(a -> Dataway b) | success(a) | Dataway b |
this allow us to chain function that can produce different variance of Dataway
import { dataway, success, failure, loading } from "dataway";
const jsonParse = (json: string) => {
try {
return success(JSON.parse(json));
} catch (error) {
return failure(error);
}
};
const checkPayload = payload => {
if (payload.hasOwnProperty("failure")) {
return failure(payload.failure);
}
return success(payload);
};
const validate = data =>
dataway.chain(dataway.chain(data, jsonParse), checkPayload);
validate(success("{ \"articles\" : []}"));
// => Success Object
validate(success("{ : []}")));
// => Failure SyntaxError
validate(success("{ \"failure\" : \"server returned 403\"}")));
// => Failure "server returned 403"
validate(loading)
// => Loading;
Allow to turn function a -> b
into a Dataway a -> Dataway b
Where Dataway b
use the same constructor value as Dataway a
f(a -> b) | Functor(a) | Result |
---|---|---|
f(a -> b) | success(a) | success(b) |
f(a -> b) | notAsked | notAsked |
f(a -> b) | loading | loading |
f(a -> b) | failure(c) | failure(c) |
Generated using TypeDoc
This type represents the four possible states of a remote datasource fetching result.
The order of transformation should look like this
NotAsked
->Loading
->Error<E>
|Success<A>