JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 11032
  • Score
    100M100P100Q137411F
  • License MIT

Common algebraïc data types for JavaScript

Package Exports

  • lemons

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (lemons) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

npm Build Status Coverage Status Minified Size

🍋 Common algebraïc data types for JavaScript, 'cause when life hands you lemons...

Maybe

Pseudo-type:

type Maybe<T>
    = Just T
    | Nothing

Usage example:

import Maybe, { Just, Nothing } from 'lemons/Maybe';
// or: import { Maybe, Just, Nothing } from 'lemons';

const r1: Maybe<number> = Just(42);
r1.isJust()         // => true
r1.isNothing()      // => false
r1.withDefault(99)  // => 42
r1.unwrap()         // => 42
r1.expect('Foo')    // => 42

const r2: Maybe<number> = Nothing();
r2.isJust()         // => false
r2.isNothing()      // => true
r2.withDefault(99)  // => 99
r2.unwrap()         // throws Error('Cannot unwrap a Nothing')
r2.expect('Foo')    // throws Error('Foo')

Result

Pseudo-type:

type Result<E, T>
    = Ok T
    | Err E

Usage example:

import Result, { Ok, Err } from 'lemons/Result';
// or: import { Result, Ok, Err } from 'lemons';

const r1: Result<string, number> = Ok(42);
r1.isOk()           // => true
r1.isErr()          // => false
r1.withDefault(99)  // => 42
r1.unwrap()         // => 42

const r2: Result<string, number> = Err('Oops');
r2.isOk()           // => false
r2.isErr()          // => true
r2.withDefault(99)  // => 99
r2.unwrap()         // throws 'Oops'

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:

How to use LazyResult in real apps