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

@mapl/pattern-router

v0.1.2

Published

A fast URLPattern router

Readme

A fast URLPattern router.

Usage

Most URLPattern API patterns are supported.

import { node_create_root, node_insert } from '@mapl/pattern-router/tree/node';
import { node_compile_to_regexp } from '@mapl/pattern-router/tree/regexp';

const root = node_create_root<string>();

// in a router implementation static patterns should be matched first
// using a Map instead of using the regexp router.
node_insert(root, '/', 0, 'home');

node_insert(root, '/forum/:forum+', 0, 'forum');

const handlers: string[] = [], 
  offsets = [1],
  regexp = new RegExp('^' + node_compile_to_regexp(root, handlers, offsets) + '$'); // /^\/(?:()|forum\/(?<forum>.+)())$/

handlers; // ["home", "forum"]
offsets; // [1, 3, 4]

const match = (path: string) => {
  let match = regexp.exec(path);
  for (let i = 0; i < handlers.length; i++) {
    // check for each empty capture group position in the regexp
    // if a path matches the capture group will capture an empty string at offsets[i]
    if (match[offsets[i]] === '') {
      return { result: handlers[i], params: match.groups };
    }
  }
}

match('/'); // { result: "home", params: { forum: undefined } }
match('/about'); // undefined
match('/forum/@mapl/pattern-router'); // { result: 'forum', params: { forum: '@mapl/pattern-router' } }

Types

To infer path parameter types of a pattern:

import type { InferParams } from '@mapl/pattern-router/tree/utils';

type T = InferParams<'/:id'>; // { id: string }
type T = InferParams<'/user/:id*'>; // { id: string | undefined }
type T = InferParams<'/book{s/:id}?'>; // { id: string | undefined }

JIT

Compile a router to matching code.

import { router_init, router_set } from '@mapl/pattern-router';
import { router_compile_to_code } from '@mapl/pattern-router/jit';

const router = router_init<string>();
router_set(router, 'GET', '/', 'return"home"');
router_set(router, 'POST', '/forum/:forum+', 'return"posting to "+match.groups.forum');

const match = Function(
  'method', 
  'path', 
  router_compile_to_code(
    router, 
    // regexp match result variable
    'match', 
    // input path variable
    'path', 
    // input method variable
    'method', 
    // additional code for HEAD method handling
    ''
  )
);

match('GET', '/'); // "home"
match('GET', '/about'); // undefined
match('POST', '/forum/@mapl/pattern-router'); // "posting to @mapl/pattern-router"

In the above example, router_compile_to_code() outputs:

switch(method){case"HEAD":{}case "GET":{switch(path){case"/":{return"Home"}break;}}break;case"POST":{let match=/^\/forum\/(?<forum>.+)()$/.exec(path);if(match!==null){if(match[2]===""){return"Posting to "+match.groups.forum}}}break;}

Limitations

Wildcards and unnamed capture groups don't capture. Use named capture groups instead:

node_insert(root, '/(\\d+)', 0, ...);
// change to
node_insert(root, '/:id(\\d+)', 0, ...);
// or without automatic group prefixing
node_insert(root, '/{:id(\\d+)}', 0, ...);

Compability

This library requires RegExp duplicate named capture groups in different disjunction feature support.