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

@fgv/ts-utils

v4.0.2

Published

Assorted Typescript Utilities

Downloads

11,885

Readme

Summary

Assorted typescript utilities that I'm tired of copying from project to project. Most notable and closest to production-ready are:

  • Result<T> - Easily combine inline and exception-based error handling
  • Converter<T> - Conversion framework especially useful for type-safe processing of JSON

Installation

With npm:

npm install @fgv/ts-utils

API Documentation

Extracted API documentation is here.

Overview

The Result Pattern

A Result<T> represents the success or failure of executing some operation. A successful result contains a return value of type T, while a failure result contains an error message of type string. Taken by itself, the use of Result<T> allows for simple inline error handling.

const result = functionReturningResult();
if (result.isSuccess()) {
    functionAcceptingT(result.value);
}
else {
    console.log(result.error);
}

Use succeed<T>() and fail<T>() to return success or failure:

function thisFunctionSucceeds(): string {
    return succeed('I succeeded!');
}

function thisFunctionFails(): number {
    return fail('Oops!  I failed');
}

Use orDefault when a failure can be safely ignored:

// returns undefined on failure
const value1: string|undefined = functionReturningResult('whatever').orDefault();

// returns 'oops' on failure
const value2: string = functionReturningResult('whatever').orDefault('oops');

The orThrow method converts a failure result to an exception, for use in contexts (such as constructors) in which an exception is the most appropriate way to handle errors.

constructor(param: string) {
    this._param = validateReturnsResult(param).orThrow();
}

The captureResult function converts an exception to a failure for simplified inline processing.

class Thing {
    static create(param: string): Result<Thing> {
        return captureResult(new Thing(param));
    }
}

Other methods and helpers allow for chaining and conversion of results, working with mulitple results and more. See the API documentation for details.

Converters

The basic Converter<T> implements a convert method which converts unknown to T, using the result pattern to report success or failure.

class Converter<T> {
    public convert(from: unknown): Result<T>;
}

But built-in converters, including converters which can extract a field for an object or which apply converters according to the shape of some object can be composed to provide compact and legible type-safe conversion from anything to a strongly typed Typescript object:

interface Thing {
    title: string;
    count: number;
    isGood: boolean;
    hints: string[];
}

const thingConverter = Converters.object<Thing>({
    title: Converters.string,
    count: Converters.number,
    isGood: Converters.boolean,
    hints: Converters.array(Converters.string),
});

// gets a Thing or throws an error
const thing: Things = thingConverter.convert(json).orThrow();

Everything is strongly-typed, so Intellisense will autocomplete properties and highlight errors in the object supplied to Converters.object.

Other helpers and methods enable optional values or fields, chaining of results and a variety of other conversions and transformations.

API

Result<T>

Converter<T>