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 🙏

© 2025 – Pkg Stats / Ryan Hefner

graphmatch

v1.1.0

Published

A low-level utility for matching a string against a directed acyclic graph of regexes.

Readme

Graphmatch

A low-level utility for matching a string against a directed acyclic graph of regexes.

  • It supports matching strings partially too.
  • It supports fine-grained control over which nodes in the graph can be matched partially or not.
  • It supports compiling the whole graph to a regex, even for partial matches.
  • The graph will always be matched against the input string from the very start.
  • RegExp flags are supported as long as they are the same for all the regexes in the graph.

Install

npm install graphmatch

Usage

import graphmatch from 'graphmatch';

// Let's say we would like to match against this glob: foo/{bar,baz}/qux
// Let's express that as a graph of regexes that this library can match against
// Whether you reuse the "qux" node or not doesn't matter, both are supported

const GRAPH = {
  regex: /foo/,
  children: [
    {
      regex: /\//,
      children: [
        {
          regex: /bar/,
          children: [
            {
              regex: /\//,
              children: [
                {
                  regex: /qux/
                }
              ]
            }
          ]
        },
        {
          regex: /baz/,
          children: [
            {
              regex: /\//,
              children: [
                {
                  regex: /qux/
                }
              ]
            }
          ]
        }
      ]
    }
  ]
};

// Let's now match against the graph, fully

graphmatch ( GRAPH, 'foo/bar/qux' ); // => true
graphmatch ( GRAPH, 'foo/baz/qux' ); // => true

graphmatch ( GRAPH, 'foo/bar/whoops' ); // => false
graphmatch ( GRAPH, 'foo/baz' ); // => false

// Let's now match against the graph, partially
// A partial match happens when any node in the graph sees the end of the string, either before or after the node's regex is executed

graphmatch ( GRAPH, 'foo/bar/qux', { partial: true } ); // => true
graphmatch ( GRAPH, 'foo/bar/', { partial: true } ); // => true
graphmatch ( GRAPH, 'foo/bar', { partial: true } ); // => true
graphmatch ( GRAPH, 'foo/', { partial: true } ); // => true
graphmatch ( GRAPH, 'foo', { partial: true } ); // => true

graphmatch ( GRAPH, 'foo/bar/whoops', { partial: true } ); // => false
graphmatch ( GRAPH, 'foo/barsomething', { partial: true } ); // => false
graphmatch ( GRAPH, 'bar', { partial: true } ); // => false

// As we just saw partial matching is set to "false" by default, and there is an option to set it to "true" by default
// But you can also decide on a node-by-node basis whether partial matching should be enabled or disabled for that particular node
// A partial node will match if the whole input string has been consumed, right before or after the node's regex is executed
// This has higher priority compared to the global setting
// This is useful for fine-grained control over which nodes can be matched partially

const NODE_NEVER_PARTIAL = { // This node will never match partially
  partial: false,
  regex: /foo/,
  children: [],
};

const NODE_ALWAYS_PARTIAL = { // This node will always match partially
  partial: true,
  regex: /foo/,
  children: [],
};

// Let's now compile the whole graph to a single regex
// This is useful if you expect to match against the graph multiple times
// It's faster to compile the graph once and match against it multiple times

const fullRe = graphmatch.compile ( GRAPH ); // => RegExp

fullRe.test ( 'foo/bar/qux' ); // => true
fullRe.test ( 'foo/bar' ); // => false

const partialRe = graphmatch.compile ( GRAPH, { partial: true } ); // => RegExp

partialRe.test ( 'foo/bar/qux' ); // => true
partialRe.test ( 'foo/bar' ); // => true

License

MIT © Fabio Spampinato