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

@hatsy/route-match

v2.0.0

Published

Route matching library

Downloads

18

Readme

Route Matching Library

NPM Build Status Code Quality Coverage GitHub Project API Documentation

Simple Pattern

import { matchSimpleRoute } from '@hatsy/route-match';

const match = matchSimpleRoute('some/long/path/to/file.txt', 'some/**/{file}');
/*
match = {
  $1: "long/path/to/",
  file: "file.txt",
}
*/

Simple pattern can only match against whole entries (files or directories). Pattern format:

  • Empty string corresponds to empty pattern.
  • / matches directory separator.
  • /* matches any route entry.
  • /{capture} captures any route entry as capture.
  • /** matches any number of directories.
  • /{capture:**} captures any number of directories as capture.
  • Everything else matches verbatim.

The path can be specified as a string, as URL, or as a PathRoute instance. The latter can be constructed by urlRoute() function.

The pattern can be specified as a string, or as a RoutePattern instance. The latter can be constructed by simpleRoutePattern() function.

URL Route

import { matchURLRoute } from '@hatsy/route-match';

const match = matchURLRoute('some-dir/long/path/to/file.html?param=value', '{root([^-]*)}-dir/**/{name}.{ext}?param');
/*
match = {
  root: "some",
  $1: "long/path/to/",
  name: "file",
  ext: "txt",
}
*/

URL pattern can match against any part of the entry name. Additionally, it can match against URL search parameters.

Pattern format:

  • Empty string corresponds to empty pattern.
  • / matches directory separator.
  • * matches a part of route entry name.
  • {capture} captures a part of entry name as capture.
  • /** matches any number of directories.
  • /{capture:**} captures any number of directories as capture.
  • {(regexp)flags} matches a part of entry name matching the given regular expression with optional flags.
  • {capture(regexp)flags} captures a part of entry name matching the regular expression with optional flags.
  • ?name requires URL search parameter to present.
  • ?name=value requires URL search parameter to have the given value.
  • Everything else matches verbatim.

The path can be specified as a string, as URL, or as an URLRoute instance. The latter can be constructed by urlRoute() function.

The pattern can be specified as a string, or as a RoutePattern instance. The latter can be constructed by urlRoutePattern() function.

Matrix Route

import { matchMatrixRoute } from '@hatsy/route-match';

const match = matchMatrixRoute('root;length=5/long/path/to;dir=1/file.html;dir=0', 'root;length/**/*');
/*
match = {
  $1: "long/path/to;dir=1/",
  name: "file",
  ext: "html",
}
*/

Matrix pattern recognizes matrix URLs.

Pattern format is the same as for URL Route with addition of attribute matchers:

  • ;name requires matrix attribute to present.
  • ;name=value requires matrix attribute to have the given value.

The path can be specified as a string, as URL, or as a MatrixRoute instance. The latter can be constructed by matrixRoute() function.

The pattern can be specified as a string, or as a RoutePattern instance. The latter can be constructed by matrixRoutePattern() function.

Advanced Usage

The library supports different route formats mentioned above. Each format represents the route as an array of entries plus additional info. The route can be constructed either manually or parsed by corresponding function.

All routes extend PathRoute interface with the following methods:

  • section(fromEntry[, toEntry]) returns a section of the route.
  • toString() converts a route to original string, including search parameters and matrix attributes.
  • toPathString() converts a route to path string, excluding search parameters and matrix attributes.

A route pattern is an array of RouteMatchers compatible with corresponding route format. Route pattern can be constructed out of rmatch... and rcapture... matchers, or parsed by corresponding function.

The routeMatch() function does the actual matching. It returns either null if the given path does not match the pattern, or a RouteMatch instance. The latter is a function that reports captured matches when called.