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

twitten

v1.2.0

Published

A fluent js/ts implementation of the result monad, utilizing Happy/Sad path terminology.

Downloads

5

Readme

Twitten - type safe & expressive error-handling

Build Status Read the Docs (version) GitHub license

NPM

Problems solved

  • Type-safe error-handling for JS & TS
  • Ubiquitous language for error-handling

Introduction

The twitten library lets you handle errors in JavaScript and TypeScript in a type safe and expressive way. The way this is done is nothing new, especially if you're familiar with how success and error cases are handled in the functional world. What's unique as far as I know is that this little library utlize 'Happy & Sad path' temrinology drawn from the world of testing.

Examples

How we usually do when we only have the try/catch + throw jump statement

function createUser(uname: string, pw: string): User  {
  if (!(uname && pw))
    throw new Error("Validation failed!");
  return { username: uname, password: pw };
}

try {
  const user = createUser("John Doe", "hello123");
  console.log(user.username, user.password);
} catch (error) {
  throw error;
}

CONS

  • This approach makes our code harder to reason about than it has to be. Since js and ts does not allow us to embedd the throw jump statement in our function and method signatures, we have to rip up the hood and read the implementation details for clues to what errors we could handle.

  • There's more to it than that! Like other jump statements the throw statement makes the control flow of our applications harder to reason about. The control flow following an expected path from module a -> b -> c... is disturbed. We could have the control go from module a -> d... or a -> b -> c -> g -> p -> x..-. There are many possibilites, and its hard to reason about!

  • Adding to the stack of hard to reasons, js and ts doesn't have the powerful catch clause that we know from languages like C#.

The Altenrative - import { Path, Happy, Sad } from "twitten"

Path with explicit type checking & unwrap

function createUser(uname: string, pw: string): Path<User>  {
  if (!(uname && pw))
    return Sad.path(new Error("Something went wrong!"));
  
  return Happy.path({ username: uname, password: pw });
}


//Must do explicit type checking before we know the correct type of outcome
const user = createUser("John Doe", "hello123");

if (Path.isHappy(user.outcome))
  console.log(user.outcome.username, user.outcome.password);
else //outcome.message refers to 'Error.message'
  console.log(user.outcome.message);

Path with continuations

const user = createUser("John Doe", "hello123");

user.onHappyPath(usr => console.log(usr.username, usr.password))
    .onSadPath(error => console.log(error.message));

Path with more continuations

const user = createUser("John Doe", "hello123");

user.onHappyPath(usr => {
        console.log(usr.username, usr.password);
        return usr;
    }).onHappyPath(usr => usr.username.replace(" ", "_"))
      .onHappyPath(uname => console.log(uname));

Conclusion

By embedding errors within the type system, we force awarness of potential failures upon consumers of our code. By doing so we make a clear distinction between unexpected and exceptional errors (exceptions) on one side, and expected errors (sad paths) on the other.

If an exception bubbles up to the end-user it will not have anything to with the expected like a input validation error, so we know that something went seriously wrong.

For documentation see