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

expressive-switch

v1.0.0

Published

Use switch statements with a custom comparison expression. Goodbye `switch(true)`!

Downloads

2

Readme

Expressive Switch

Use switch statements with a custom comparison expression. Goodbye switch(true)!

Very small library with no dependencies.

const switchPair = require('expressive-switch');

// Set up the closure and return the helper functions
const [s, c] = switchPair();

const subject = "I love horses";

switch(s(testCase => subject.indexOf(testCase) > -1)) {
	case c('ducks'):
		// This one does't match
		console.log("Someone's talking about ducks");
		break;
	case c('horses'):
		// This one matches!
		console.log("Someone's talking about horses");
		break;
	default:
		// Fallback for no matches
		console.log("Who knows?");
}

Uses

Error checking

function handleError(error) {
	const [s, c] = switchPair();

	switch(s(cls => error instanceof cls)) {
		case c(NotFoundError):
			return 'Page not found!';
		case c(TypeError):
			return 'Provided the wrong type!'
		default:
			// Don't know what the problem is, throw it
			throw error;
	}
}

URL routing

function routeRequest(url) {
	const [s, c] = switchPair();
	let args;

	switch(s(regex => args = url.match(regex))) {
		case c(/^\/$/):
			return HomePageComponent();
		case c(/^\/photos\/([0-9]+)$/):
			return PhotographComponent({ photoId: args[1] });
		default:
			return PageNotFoundComponent();
	}
}

Shape checking

function castResponseObject(object) {
	// Alternative syntax - specify the subject and predicate separately
	const [shapeOf, hasKey] = switchPair((key, subject) => key in subject);

	switch(shapeOf(object)) {
		case hasKey('error'):
			return new Error(object.error);
		case hasKey('value'):
			return object.value;
		default:
			return object;
	}
}

How does it work?

s saves your predicate function in a closure and returns a symbol.

c calls the predicate function with the test case value and returns the same symbol if the predicate returns truthy.

As far as switch is concerned, it's dealing with a symbol. As far as case is concerned, it's dealing with that same symbol or null.

TC39 Proposal

What if this was more concise and part of the javascript core language?

function handleError(error) {
	switch(error instanceof case) {
		case NotFoundError:
			return 'Page not found!';
		case TypeError:
			return 'Provided the wrong type!'
		default:
			throw error;
	}
}

Maybe it could be!