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

@cakasim/smart-results

v0.1.0

Published

Simple library for avoiding use of exceptions.

Downloads

19

Readme

Smart Results for JS

LICENSE

Introduction

This is just a small library to make error handling somewhat joy in TypeScript. It is heavily inspired by Rust and its great concepts about error handling.

How to Use?

Currently there are two types you can mak use of:

  • Option<T> which is a union type of Some<T> and None<T>
  • Result<T, E> which is a union type of Ok<T, E> and Err<T, E>

The type Option<T> comes with two helper functions, some(value) and none(). You should use these functions to create an Option<T>. The type Result<T, E> follows the same principle except that you will have ok(value) and err(value) to create it.

The following code provides a brief overview which helps understanding the concepts.

import { Option, some, none, Result, ok, err } from '@cakasim/smart-results';

// Just a simple type for us to play with
type Animal = {
  name: string;
};

// Another type which holds metrics about an animal
type Metric = {
  minSize: number;
  maxSize: number;
  isCarnivorous: boolean;
  cutenessFactor: number;
};

// A pre-filled list of animals
const animals = new Map<number, Animal>([
  [1, { name: 'Ape' }],
  [2, { name: 'Dog' }],
  [3, { name: 'Cat' }],
  [4, { name: 'Pig' }],
  [5, { name: 'Bird' }],
])

/**
 * Finds an animal in the list and returns an Option<Animal>
 * which holds a valid animal if we have found an entry or nothing
 * if there is no entry for the provided ID.
 *
 * @param id Animal ID.
 */
function findAnimal(id: number): Option<Animal>
{
  // Get animal for provided ID, or undefined if ID is invalid
  const animal = animals.get(id);

  // Check if animal is not undefined and return either Some or None
  return animal
    ? some(animal)
    : none();
}

/**
 * Fetches a metric dataset about the provided animal from an HTTP Rest API.
 * The return value is a Promise that (always) resolves to a Result<Metric, Error>.
 * If a metric could be loaded the result contains a valid Metric value.
 * Otherwise the result contains an Error value.
 *
 * @param animal The animal.
 */
async function fetchAnimalMetric(animal: Animal): Promise<Result<Metric, Error>>
{
  // Assume we are fetching actual data here
  const response = await fetchAnimalMetricFromRestApi(animal);

  // Check if the response code is good and return either Ok or Err
  return response.statusCode === 200
    ? ok(response.data as Metric)
    : err(new Error(`Could not fetch from HTTP API, go status code ${response.statusCode}.`));
}

/**
 * This is the function that gets actually executed by someone.
 *
 * @param animalId Animal ID.
 */
async function printAnimalMetric(animalId: number)
{
  // Search for the animal, *expect* a None result and handle it by throwing the provided Error.
  // If the result is Some no error will be thrown. Please note that `expect()` should be used
  // carefully only if the exception case can be handled properly.
  const animal = findAnimal(animalId).expect(() => new Error(`Cannot find animal for ID ${animalId}.`));

  // metric is a Result<Metric, Error>, at this point we are forced to check for a valid result
  const metric = await fetchAnimalMetric(animal);

  if (metric.isErr()) {
    // Handle the error case
    console.error(`Failed to fetch data for animal ID ${animalId} with name ${animal.name} from REST API: ${metric.value.message}`);
  } else {
    // At this point metric.value is safe to use
    console.log(`The metric for animal ID ${animalId} with name ${animal.name} is:`);
    console.log(metric.value);
  }
}