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

tryless

v1.3.0

Published

A simple Result type for TypeScript. Inspired by Rust's Result type.

Downloads

52

Readme

Here is an improved version of your README:

TryLess

TryLess is a TypeScript library designed to simplify error handling and minimize the use of try-catch blocks. The library introduces Result and Future types that encapsulate both success and error states, along with a set of helper functions to streamline the handling of these results.

Why Avoid Extensive Use of Try-Catch Blocks?

While try-catch blocks are a common method for handling errors in JavaScript and TypeScript, they can make your code verbose and difficult to read, particularly when dealing with multiple nested try-catch blocks. Consider the following example:

async function registerUser(user: User): Promise<void> {
  const email = user.email;

  try {
    const user = await getUserByEmail(email);
  } catch (error) {
    console.error(error);

    if ((error as any).message !== "User not found") {
      try {
        const notifyResult = await notifyAdmin(`Failed to get user by email: ${error}`);
      } catch (notifyError) {
        console.error(notifyError);
        throw new Error(`Failed to notify admin: ${notifyError}`);
      }

      throw new Error(`Failed to get user by email: ${error}`);
    }
  }

  if (user) {
    throw new Error('User already exists');
  }

  try {
    const createUserResult = await createUser(user);
  } catch (createUserError) {
    console.error(createUserError);
    throw new Error(`Failed to create user: ${createUserError}`);
  }
}

This code is hard to read and maintain due to the nested try-catch blocks and the need to handle different types of errors. TryLess provides a more elegant and concise way to handle errors using the Result and Future types and utility functions.

Simplifying with TryLess

If the functions getUserByEmail and createUser return a Result type, the code can be simplified using the Future type and utility functions provided by TryLess.

Example:

import { ok, err, Future } from 'tryless';

async function registerUser(user: User): Future<void, string> {
  const email = user.email;
  const userResult = await getUserByEmail(email);

  if (userResult.isError()) {
    const userError = userResult.reason;
    console.error(userError);

    if (userError.message !== "User not found") {
      const notifyResult = await notifyAdmin(`Failed to get user by email: ${userError}`);

      if (notifyResult.isError()) {
        return err(`Failed to notify admin: ${notifyResult.reason}`);
      }

      return err(`Failed to get user by email: ${userError}`);
    }
  }

  const user = userResult.value;

  if (user) {
    return err('User already exists');
  }

  const createResult = await createUser(user).unwrapAll();

  if (createResult.isError()) {
    const createUserError = createResult.reason;

    console.error(createUserError);

    return err(`Failed to create user: ${createUserError}`);
  }

  return ok();
}

This code is much easier to read and understand. The Future type encapsulates both success and error states, allowing you to handle both cases in a single return statement. The ok and err functions create success and error results, respectively, and the await keyword is used to wait for the results of asynchronous operations.

Features

  • Encapsulates success and error states in a single type.
  • Provides utility functions to handle results without try-catch blocks.
  • Converts regular and async functions to return results.
  • Extends promises to return results.

Installation

Install the library using npm:

npm install tryless

Getting Started

To get started with TryLess, follow these simple steps:

  1. Install the library using the npm command above.
  2. Import the necessary functions and types (ok, err, Future) from the library.
  3. Refactor your functions to return Result or Future types instead of throwing errors.
  4. Use the utility functions to handle results in a concise and readable manner.

With TryLess, you can make your error handling code cleaner, more maintainable, and easier to understand.

Contributing

Contributions are welcome! If you have suggestions for improvements or new features, please create an issue or submit a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.