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

notry-ts

v0.0.2

Published

An abstraction on top of try-catch making exceptions type safe

Downloads

3

Readme

notry-ts

An abstraction on top of try-catch making exceptions type safe.

Installation

npm install --save notry-ts

Overview

Define functions that accept a Quit parameter specifying how it can fail. Call with notry to handle errors and return a discriminated union type Did prepresenting the result.

Usage

Import the notry function and Quit type.

import { notry, type Quit } from "notry-ts";

quit

Define a function that could fail. Use Quit to specify possible error types and call quit to throw.

/**
 * Return a random number between 0 and max.
 * Could fail with "Bad max" or "Out of range".
 */
function randomUnder(
  quit: Quit<"Bad max" | "Out of range">,
  max: number
): number {
  if (max > 1) {
    quit("Bad max");
  }
  const val = Math.random();
  if (val > max) {
    quit("Out of range");
  }
  return val;
}

notry

Use notry to run a quitable function and return a Did. Note that if the function throws without using quit, it will not be handled by notry.

// call randomUnder(0.9) and log the result
const did = notry(randomUnder, 0.9);
if (did.ok) {
  // did.y: number
  console.log(did.y);
} else {
  // did.n: "Bad max" | "Out of range"
  console.error(did.n);
}

Did

If the function returns normally, did.ok is true and did.y holds the return value. Otherwise if quit is called, did.ok is false and did.n holds the value it was called with.

quit.if

Convenience function for quitting on some condition. May not be appropriate if type narrowing is required.

function randomUnder(
  quit: Quit<"Bad max" | "Out of range">,
  max: number
): number {
  quit.if(max > 1, "Bad max");
  const val = Math.random();
  quit.if(val > max, "Out of range");
  return val;
}

quit.catch

Convenience function to map external exceptions to quit space.

/**
 * Return a random number between 0 and json.max.
 * Could fail with "Bad config" or "Bad max" or "Out of range".
 */
function randomFromJson(
  quit: Quit<"Bad config" | "Bad max" | "Out of range">,
  json: string
): number {
  const obj = quit.catch(JSON.parse, json, "Bad config");
  if (
    typeof obj !== "object" ||
    !obj ||
    !("max" in obj) ||
    typeof obj.max !== "number"
  ) {
    quit("Bad config");
  }
  const { max } = obj;
  quit.if(max > 1, "Bad max");
  const val = Math.random();
  quit.if(val > max, "Out of range");
  return val;
}

// call randomFromJson('{"max":0.9}') and log the result
const did = notry(randomFromJson, '{"max":0.9}');
if (did.ok) {
  // did.y: number
  console.log(did.y);
} else {
  // did.n: "Bad config" | "Bad max" | "Out of range"
  console.error(did.n);
}

nested errors

Split randomFromJson into two functions, either of which could fail.

import { notry, type Of, type Quit } from "notry-ts";

/**
 * Return the value of json.max
 * Could fail with "Bad config".
 */
function maxFromConfig(quit: Quit<"Bad config">, json: string): number {
  const obj = quit.catch(JSON.parse, json, "Bad config") as unknown;
  if (
    typeof obj !== "object" ||
    !obj ||
    !("max" in obj) ||
    typeof obj.max !== "number"
  ) {
    quit("Bad config");
  }
  return obj.max;
}

/**
 * Return a random number between 0 and json.max.
 * Could fail with a maxFromConfig error or "Bad max" or "Out of range".
 */
function randomFromJson(
  quit: Quit<Of<typeof maxFromConfig> | "Bad max" | "Out of range">,
  json: string
): number {
  const max = maxFromConfig(quit, json);
  quit.if(max > 1, "Bad max");
  const val = Math.random();
  quit.if(val > max, "Out of range");
  return val;
}

const did = notry(randomFromJson, '{"max":0.9}');
if (did.ok) {
  console.log(did.y);
} else {
  console.error(did.n);
}

async

notry and quit.catch both support async functions.