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

rop-monads

v0.0.3

Published

Provides Result and Optional monads, inspired by railway oriented programming.

Downloads

52

Readme

rop-monads

Getting started

npm install rop-monads

or

yarn add rop-monads

Using rop-monads

Optional

import { Optional } from 'rop-monads';

const age: Optional<number> = Optional.of(getPerson()) // getPerson() returns an optional object that contains an age
  .map(person => person.age);

Result

import { Result } from 'rop-monads';

const todos: Result<Todo[], CustomError> = apiCall()// returns a result
  .flatMap(json => transformJson(json)) // the transform function can return a result as the json decoding can fail

todos.match({
  ok: todos => handleTodos(todos),
  err: failure => handleFailure(failure)
});

Using Result in HTTP controllers for example with express.js

Error.ts

type NotFoundError = {
  kind: "NOT_FOUND";
  reason: string;
};

type InternalError = {
  kind: "INTERNAL";
  reason: Error;
};

type PersonError = NotFoundError | InternalError

PersonController.ts

const personService = getPersonService();

function httpHandleFailure(res: express.Response, failure: PersonError) {
  switch (failure.kind) {
    case "NOT_FOUND":
      log("failed to find person it does not exist", failure.reason);
      return res.status(404).send({ status: 404, message: "person does not exist" });
    case "INTERNAL":
      log("failed to find person", failure.reason.message);
      return res.status(500).send({ status: 500, message: "something went wrong" });
  }
}

// assumed that all functions used are not async
app.get('/person/:id', (req, res) => {
  getIdFromParams(req) // returns a Result
  .flatMap(id => personService.findPerson(id))
  .map(person => encodePerson(person))
  .match({
    ok: personJson => res.status(200).send(personJson),
    err: failure => httpHandleFailure(res, failure)
  });
});

For more on how to use this library checkout our API documentation.

Contribute

We welcome pull requests. Learn how to contribute.

License

rop-monads is MIT licensed.