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

highly-questionable

v1.1.1

Published

A library for paranoid JavaScript developers

Downloads

21

Readme

Highly Questionable

A TypeScript / JavaScript library for paranoid developers.

Caution, experimental!

Highly Questionable allows you to safely and elegantly handle values that might be null, undefined or Errors, without writing tedious null checks and try-catches everywhere. It is loosely based on the Option, Maybe and Result monads from other languages.

Think of it like a synchronous Promise, with map/catch methods equivalent to then/catch. 'Map' won't be called if the value doesn't exist, and you can handle errors with catch.

Concept

Values are wrapped in a Perhaps object. When you want to use the value, you pass perhaps 'mapping' functions that are only run if the value is valid. The output of the 'mapping' gets wrapped in a new Perhaps, and so on, so forth, like so:

const userName = Perhaps.of(userJSON)
    .map(json => JSON.parse(json))
    .map(user => user.details)
    .map(details => details.name);

There are three things that could normally fail here: JSON.parse might throw an error; the user may not have details; or details may not have a name. Perhaps will handle each one gracefully.

When you need to actually use a value, there are various methods and checks to make this safe:

userName.forOne(name => {
    // This only runs if userName exists and contains no errors
    print(name);
});

if (userName instanceof Something) {
    // If using TypeScript, the compiler will now know the unwrapped value is safe
    print(userName.unwrap());
}

if (userName !== Nothing) {
    // 'catch' is another way to handle errors, and works like Promise.catch
    const unwrapped = userName.catch(error => 'Unretrievable').unwrap();
    print(unwrapped);
}

print(userName.unwrapOr('Anonymous'));

You can also catch exceptions at your leisure:

userName.catch(err => {
    logException(err);
    return 'Anonymous'; //  can pass a default to use
});

And throw exceptions when values don't exist:

userName.unwrapOrThrow(new Error('Could not retrieve user name'));

Note that every Perhaps object is immutable: mapping one will create a new instance, unless the output is Nothing (which is a singleton, as all Nothings are the same).

For more details, consult the API docs.

Setup & usage

Installing the package:

npm install highly-questionable

Importing the code:

// TypeScript / ESNext
import {Perhaps, Something, Nothing, Problem} from 'highly-questionable';
// Node
const {Perhaps, Something, Nothing, Problem} = require('highly-questionable');

TypeScript should work out of the box.

API

See API.md.

Due dilligence

License

This library is provided under an Apache 2.0 license. For more details, see [LICENSE.md];

Dependencies

This project has no production dependencies.

Library size

At v1.1, the whole library minified and gzipped amounted to 1018 bytes.

Contributing / developing

  1. Check out the code and install Node
  2. Use npm install to install project dependencies
  3. Write your TypeScript
  4. Test with npm test
  5. Build the bundle with npm run build
  6. Raise a pull request