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

matchbook

v1.0.2

Published

Declarative function overloading

Downloads

13

Readme

Build Status

Matchbook

Like switch on runtime steroids, for easily creating overloaded functions.

# CoffeeScript
pattern = 'matchbook'

myFunc = pattern (match) ->
  # TODO: interesting example
// ES 6
let pattern = require('matchbook');

let myFunc = pattern(match => {
  // TODO: interesting example
});
// ES 5
var pattern = require('matchbook');

var myFunc = pattern(function(match) {
  // TODO: interesting example
});

Patterns

Valid patterns are:

  • "classes" (constructor functions)
    • The built-in functions Number, String, Boolean, Function, and Array are special-cased.
  • arbitrary predicates: If(x => x > 0)
  • arrays of patterns: [String, Number, Int]
  • objects of patterns: { foo: String, bar: Function }
  • literal values: 1, null, true, void 0, etc.

Note that the special-cased primitive patterns (like Number and String) only match primitive values (like 123 and foo), not boxed values (like new Number(123) and new String('foo')).

API

Matchbook exports a function, which I'll call pattern.

let pattern = require('matchbook');

Attached to pattern are some other functions:

  • pattern.compile(pattern)
    • converts a pattern into a predicate -- a function that tests that pattern on the given value
    • this is not necessary if you're using the pattern function directly, but can be useful in other cases
  • pattern.Any
    • a pattern that matches any value, including undefined
  • pattern.If(predicate)
    • returns a predicate-based function
  • pattern.List(pattern)
    • returns a pattern that matches on iterators (such as Arrays, and generators) only if each element matches the supplied pattern
  • pattern.Or(pattern1, pattern2)
    • returns a pattern that succeeds if either supplied pattern matches
  • pattern.And(pattern1, pattern2)
    • returns a pattern that matches if both supplied patterns match
    • example: And(Number, If(x => x > 0))
  • pattern.overload
    • a synonym for pattern, for more convenient destructuring imports.

Extracting values from patterns

This is not supported, and cannot be implemented in vanilla JavaScript without some horrible abuse of eval. If it's an option for you, consider a SweetJS-based pattern matcher like Sparkler instead.

Default values, spreads, and views

There is no support yet for default values of functions, nor for spreads, nor for views. But these would not be hard to add, and I'd be happy to do so if there's interest. Or I'll just go ahead and do it if I encounter a need myself.

Compatibility

Matchbook will not work in IE 8 or below.

Changelog

License