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 🙏

© 2026 – Pkg Stats / Ryan Hefner

patjs

v0.1.6

Published

Pattern matching in JavaScript

Downloads

181

Readme

Pattern matching in JavaScript

Getting Started

var pat = require('pat');

var pow = pat()
  .caseof(Number, 0, function() { return 1; })
  .caseof(Number, Number, function(x, y) { 
    return x * pow(x, y - 1 ); 
  });

Why pattern matching?

What is pattern matching? In short, it is a mechanism to choose which variant of a function is the correct one to call.

Think about a pow(base, exponent) function. In fact, pow has three variants:

  • If the exponent is 0, return 1.
  • If the exponent is greater than zero, return the 'normal' base to the exponent power.
  • If the exponent is less than 0, return 1 divided by the base to the exponent power.

When pow is called, the function has to choose which variant to use. Traditionally, this is done by if-else comparison. Here's an example of pow implementation:

function pow1(x, y) {
  if(y === 0) {
    return 1;
  } else if(y < 0) {
    return 1 / x * pow1(x, ((y * -1) - 1));
  } else {
    return x * pow1(x, y - 1);
  }
}

However, pattern matching let's you get rid of the cubersome ifs and replace those in more elegant caseof control structure.

function lessThan(a) {
  return function(b) {
    return b < a;
  }
}

var pow2 = pat(function(x, y) {
    return x * pow2(x, y - 1); 
  })
  .caseof(Number, 0, function() {
    return 1;
  })
  .caseof(Number, lessThan(0), function(x, y) {
    return 1 / x * pow2(x, ((y * -1) - 1));
  });

Why pattern matching in JavaScript?

JavaScript is dynamically typed language, which means (among other things) that functions can take any number of arguments of any type. The fact that any type is allowed, means that no one warns you if you pass in wrong type. For example, if you pass in String when int was expected, you might not notice an error if you don't look carefully. Take a sum function as an example:

function sum(a, b) {
  return a + b;
}

Obviously, the sum is intended to be used with ints, but if you pass in String, you see no error but a weird result.

sum(1, 2) === 3
sum("1", "2") === "12"

Dynamic typing has both advantages and disadvantages. The above example, lack of compiler warnings demonstrates one of the disadvantages. However, the fact that any argument can be passed to function can be also convenient. We JavaScript devs have learned how to use this to write APIs that can take "almost" any argument, and work properly. For example, it's not too hard to implement the sum function so that it in fact could work properly with a string or an array of ints or maybe even array of strings.

However, implementing the functions to work with multiple different types of arguments is not very interesting coding task. For example, in the sum function, the real beef of the functions is the a + b. However, if you'd implement sum to liberally take strings and arrays and whatnot, you'd end up writing 10 lines of if-elses to just get the arguments right.

(To be continued...)

Examples

See examples/

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using grunt.

Inspiration

License

Copyright (c) 2013 Mikko Koski
Licensed under the MIT license.