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

exceptnomore

v1.2.14

Published

Result and Optional types that will make your code exception-proof

Downloads

4

Readme

exceptnomore

Result and Optional types that will make your code exception-proof.

This library allows you to wrap Promises, async functions and normal functions in objects that will wrap those exceptions for a cleaner code with less try/catches and a better and more explicit Error Handling.

NPM Package

npm install exceptnomore

Full documentation:

Highlights

Mapping and FlatMapping + if callbacks

Result.map

functionThatReturnsTheUserIdAsAResult()
    .map(id => getUserData(id))
    .ifOk(u => console.log(`Hello ${u.firstName}`))
    .ifErr(err => console.debug(err));
    // if err exists, it is the original Error returned by 'functionThatReturnsTheUserIdAsAResult'

Result.flatMap

functionThatReturnsTheUserIdAsAResult()
    .flatMap(id => mayFailGetUserData(id))
    .ifOk(u => console.log(`Hello ${u.firstName}`))
    .ifErr(err => console.debug(err));
    // if err exists, it is either the content of the Error Result of 'functionThatReturnsTheUserIdAsAResult'
    // or, if that successfully returned a userId, the Error Result of 'mayFailGetUserData'

flatMap and map rules also apply to the Optional type:

Optional.map

functionThatReturnsTheUserIdAsAnOptional()
    .map(id => getUserData(id))
    .ifPresent(u => console.log(`Hello ${u.firstName}`))
    .ifEmpty(() => console.debug('Not found'));

Optional.flatMap

functionThatReturnsTheUserIdAsAnOptional()
    .flatMap(id => mayNotFindUserData(id))
    .ifPresent(u => console.log(`Hello ${u.firstName}`))
    .ifEmpty(() => console.debug('Not found'));
    // the callback passed to ifEmpty gets called if
    // 'functionThatReturnsTheUserIdAsAnOptional'
    // or 'mayNotFindUserData' return an Empty Optional

Example use cases:

A function call that may throw an exception

Before - the caller is not aware that the function may throw an exception:

function evil(): number {
    const n = Math.random();
    if (n < .5) {
        throw new Error('Ooops');
    } else {
        return n;
    }
}

console.log(evil()); // This statement can crash

After - the return type explicitly tells the caller that the function may return an error (also telling the type of it):

function evil(): Result<number, Error> {
    const n = Math.random();
    if (n < .5) {
        return Result.err(new Error('Ooops'));
    } else {
        return Result.ok(n);
    }
}
evil().ifOk(n => console.log(n)); // This statement will never crash

// Also, if you want to manage the error
evil()
    .ifOk(n => console.log(n))
    .ifErr(e => console.debug(e));

// Or, to be more concise
evil().if(n => console.log(n), e => console.debug(e));

A function that may return the object we are looking for or null (for example, when looking for a record in a database table)

Before:

function find(id: number): { name: string }|null {
    return Math.random() * 10 < id
        ? null
        : { name: 'Test User' };
}

console.log(find(5).name); // Compiler error that forces you to handle the null case if you are using TypeScript, "TypeError: Cannot read property 'name' of null" at runtime if you are not using a type-checker.

After:

function find(id: number): Optional<{ name: string }> {
    return Optional.ofNullable(
        Math.random() * 10 < id
            ? null
            : { name: 'Test User' }
    );
}

find(5).ifPresent(u => console.log(u.name)); // This statement will never crash

// Also, if you want to manage the Empty case
find(5)
    .ifPresent(u => console.log(u.name))
    .ifEmpty(() => console.log('User not found'));

// Or, to be more concise
find(5)
    .if(u => console.log(u.name), () => console.log('User not found'));