Giter VIP home page Giter VIP logo

monads's Introduction

@faergeek/monads

Easy to use monads for JavaScript and TypeScript.

Source code itself is pretty small, but it has jsdoc comments, which should serve as a more complete API documentation:

  • Maybe

    An abstract box representing either presence or absence of a value.

    A box with a value can be created with Maybe.Some(<value>). Maybe.None represents an absence of a value.

    Read the comments in source file linked above for details.

  • Async

    An abstract box representing asynchronous value state. Very similar to Maybe, but has less operations. It exists to make sure that presence/absence of a value and pending/ready state are easy to tell apart. Very useful to convey loading state through any value transformations.

    A box with a value ready to be used can be created with Async.Ready(<value>). Async.Pending represents a pending state meaning there's no value yet.

    Read the comments in source file linked above for details.

  • Result

    An abstract box representing success or failure.

    A box representing success can be created with Result.Ok(<value>). A box representing failure can be created with Result.Err(<error>).

    Read the comments in source file linked above for details.

An example of using both Async and Result to represent different states of UI depending on API request result from a hook like SWR or React Query.

First, we need to create a box, representing 3 states: pending, success and failure.

// let's assume data is something like a number 21
const { data, error, isLoading } = useSomeApiHook();

const halfTheAnswer = data
  ? Async.Ready(Result.Ok(data))
  : isLoading
    ? Async.Pending
    : Async.Ready(Result.Err(error));

Then apply transformations with map* or flatMap* functions.

Here we simply multiply the value by 2. But the same approach can be used to extract only some pieces of response data, transform them and pass to child components, all without loosing request state attached to it.

const theAnswer = halfTheAnswer.mapReady(
  // this function is only called if `halfTheAnswer` represents ready state
  result =>
    result.mapOk(
      // this function is only called if `result` represents success state
      x => x * 2,
    ),
);

And finally use the value, explicitly handling all cases. In this example, we simply render a piece of UI with something like React.

return theAnswer.match({
  Pending: () => <h1>Fetching the answer, please wait...</h1>,
  Ready: result =>
    result.match({
      Err: error => <h1>Could not get the answer: {error.message}</h1>,
      Ok: data => <h1>The Answer is {data}</h1>,
    }),
});

monads's People

Contributors

dependabot[bot] avatar faergeek avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

monads's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Awaiting Schedule

These updates are awaiting their schedule. Click on a checkbox to get an update now.

  • chore(deps): lock file maintenance

Edited/Blocked

These updates have been manually edited so Renovate will no longer make changes. To discard all commits and start over, click on a checkbox.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

asdf
.tool-versions
  • node 22.6.0
github-actions
.github/workflows/checks.yml
  • actions/checkout v4
  • asdf-vm/actions v3
  • pnpm/action-setup v3
  • actions/setup-node v4
.github/workflows/commitlint.yml
  • actions/checkout v4
  • asdf-vm/actions v3
  • pnpm/action-setup v3
  • actions/setup-node v4
.github/workflows/release.yml
  • actions/checkout v4
  • asdf-vm/actions v3
  • pnpm/action-setup v3
  • actions/setup-node v4
npm
package.json
  • @commitlint/cli ^19.0.0
  • @commitlint/config-conventional ^19.0.0
  • @faergeek/eslint-config ^6.1.0
  • @types/node ^20.11.16
  • eslint ^8.56.0
  • prettier ^3.2.5
  • rimraf ^6.0.0
  • semantic-release ^24.0.0
  • typescript ^5.3.3
  • vitest ^2.0.0
  • pnpm 9.8.0

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.