🍋 Common algebraïc data types for JavaScript, 'cause when life hands you lemons...
Result
Pseudo-type:
type Result<E, T> =
| Ok T
| Err E
Usage example:
import { Result, Ok, Err } from 'lemons';
const r1: Result<string, number> = Ok(42);
r1.isOk()
r1.isErr()
r1.withDefault(99)
r1.unwrap()
const r2: Result<string, number> = Err('Oops');
r2.isOk()
r2.isErr()
r2.withDefault(99)
r2.unwrap()
LazyResult
Pseudo-type:
type LazyResult<E, T> =
| Initial
| Loading
| Failure E
| Success T
Useful for state management that typically has an initial, loading, and
a failure/success outcome state, like page loading, or submitting a form. The
following example shows how you would use the LazyResult as part of a React
app, but since ADTs are simple data structures, they work with any technology.
Annotated usage example: