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

gbye

v4.0.0

Published

Checked exceptions for TypeScript

Downloads

3

Readme

gbye

TypeScript library combining the convenience of throw with the type safety of return codes.

Installation

npm install --save gbye

Overview

The main function run receives an operation, wraps it in a try-catch, and passes it a gbye function. The second argument to run is a handler handles typesafe exceptions for operation. Invoking gbye with the parameter type expected by handler, immediately terminates operation and invokes handler. run returns the result of operation on success, or else the result of handler. Exceptions throw without a gbye are considered impolite and not be caught by run.

Usage

Import the run function

import { run } from "gbye";

Pass an operation and channels to run. The operation is passed a Gbye object with two methods: exit and trap. Use exit to terminate the operation (think throw) via the specified channel (think catch).

run(
  // operation
  (gbye) => gbye("done"),
  // handler
  (reason: string) = > console.log("Done")
);
// logs: "Done"

A more useful example: terminating an operation conditionally.

run(
  // operation
  (gbye) => {
    if (!confirm("Do something?")) {
      gbye("Aborted");
    }
    console.log("Completed");
  },
  // handler
  (reason: string) => console.error(reason)
);
// logs: "Aborted" or "Completed"

Terminating an operation with arguments.

run(
  // operation
  (gbye) => {
    const password = prompt("Enter your password") || "";
    if (password !== "pa$$word") {
      gbye({
        reason: "Wrong password",
        data: password,
      });
    }
    console.log("Logged in");
  },
  // handler
  (err: { reason: string; data: string }) => {
    console.error(`${err.reason}: ${e.data}`);
  }
);
// logs: "Logged in" or "Wrong password: ..."

Trap exceptions thrown by other functions.

import { run, trap } from "gbye";

run(
  // operation
  (gbye) => {
    const json = prompt("Enter JSON string") || "";
    const obj = trap(gbye, () => JSON.parse(json), "Parse Error");
    console.log("Object:", obj);
  },
  // handler
  (reason: string, error?: unknown) => console.error(`${Reason}:`, error)
);
// logs: "Object: ..." or "Parse Error: SyntaxError: ..."

Define nested operations.

import { run, trap, Gbye } from "gbye";

type ParseFail = {
  type: "parse";
};

type ValidationFail = {
  type: "validation";
  detail: string;
};

/**
 * Could terminate via "parse" channel
 */
function getJSON(gbye: Gbye<ParseFail>) {
  const json = prompt("Enter JSON string") || "";
  return trap(gbye, () => JSON.parse(json), { type: "parse" });
}

/**
 * Could terminate via "invalid" channel
 */
function validate(gbye: Gbye<ValidationFail>, json: unknown) {
  if (typeof json !== "object") {
    gbye({ type: "validation", detail: "not an object" });
  }
}

run(
  // operation
  (gbye) => {
    const json = getJSON(gbye);
    validate(gbye, json);
    console.log("Object:", json);
  },
  // handler
  (val: ValidationFail | ParseFail, error?: unknown) => {
    if (val.type === "parse") {
      console.error("Parse Error:", error);
    } else if (val.type === "validation") {
      console.error("Invalid JSON:", val.detail);
    }
  }
);
// logs: "Object: ..." or "Parse Error: SyntaxError: ..." or "Invalid JSON: not an object"

API

WIP: in the meantime check out the source, it's short