npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@faergeek/monads

v1.1.0

Published

Easy to use monads for JavaScript and TypeScript

Downloads

21

Readme

@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>,
    }),
});