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

xmatch

v1.3.0

Published

Simple pattern matching for ES6 (no transpilation!)

Downloads

11

Readme

xmatch

Simple pattern matching for ES6 (no transpilation!)

Property matching

const { match } = require('xmatch');

match(obj, [
	({ x }) => console.log('x', x),
	({ y }) => console.log('y', y),
	({ z }) => console.log('z', z),
	// Exhaustive match; will throw `xmatch.UnmatchedPatternError` unless uncommented:
	// other => console.error('Something else', other),
]);

Iterable matching

const { match } = require('xmatch');

match(arr, [
	([]) => 'empty',
	([x]) => `x=${x}`,
	([x, y]) => `x=${x},y=${y}`,
	([x, y, ...{ length }]) => `x=${x},y=${y},rest.length=${length}`,
]);

Custom guards

const { match, guard } = require('xmatch');

match(obj, [
	({ command }) => {
		// When you want to match simple values:
		guard(command === 'ignore');
		/* Do nothing */
	},
	({ command }) => {
		// Or, say, match result of regex:
		let [, name, args] = guard(command.match(/^(\w+):(.*)$/));
		console.log({ name, args });
	},
	({ command }) => {
		throw new Error(`Invalid command: ${command}`);
	},
]);

Shape assertions

const { guard } = require('xmatch');

const { x, y } = guard({ x: 1, y: 2 }); // OK
const { x, y } = guard({ x: 1, z: 2 }); // throws `xmatch.UnmatchedPatternError`

Known issues

  • You can't use literals directly in patterns (this is limitation of ES6 syntax, can be fixed as part of https://github.com/tc39/proposal-pattern-matching).
  • You can't use default values for parameters. This is limitation of the way matching is implemented, and you'll have to resolve defaults yourself if that's what you want.
  • Trying to further destructure or access undefined properties of an object will also trigger the match guard (#1). This is tricky to workaround without changing the syntax, but I'll look into it (and happy to hear any suggestions).
  • This uses dynamic metaprogramming via Proxy which might have undesirable performance effect on hot code paths. If your benchmarks suggest it's causing critical performance issues, consider using transpiler plugins instead.
  • Proxy is not implemented in pre-ES6 browsers and can't be polyfilled, so use this only if you're okay with the supported target set: https://caniuse.com/#feat=proxy