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

route-parser-ts

v1.1.0

Published

A TypeScript URL routing library with support for named parameters, splats, and optional segments

Readme

route-parser-ts

A TypeScript URL routing library that parses route patterns and matches them against URL paths. Supports named parameters, splat (wildcard) parameters, and optional segments.

Based on route-parser by Ryan Sorensen.

Installation

npm install route-parser-ts

Usage

import Route from 'route-parser-ts';

// Create a route
const route = Route('/users/:id');

// Match a path
const result = route.match('/users/123');
// => { id: '123' }

// Reverse a route
const path = route.reverse({ id: '456' });
// => '/users/456'

Route Patterns

Static Paths

Match exact paths:

const route = Route('/foo');

route.match('/foo');      // => {}
route.match('/foo?query'); // => {} (query strings are ignored)
route.match('/bar');      // => false
route.match('/foobar');   // => false

Named Parameters

Capture path segments with :param syntax:

const route = Route('/users/:id');

route.match('/users/123');  // => { id: '123' }
route.match('/users/abc');  // => { id: 'abc' }
route.match('/users/');     // => false

Multiple parameters:

const route = Route('/users/:userId/posts/:postId');

route.match('/users/1/posts/42');
// => { userId: '1', postId: '42' }

Splat Parameters

Capture multiple path segments with *param syntax:

const route = Route('/files/*path');

route.match('/files/images/photo.jpg');
// => { path: 'images/photo.jpg' }

Multiple splats:

const route = Route('/*a/foo/*b');

route.match('/zoo/woo/foo/bar/baz');
// => { a: 'zoo/woo', b: 'bar/baz' }

Mixed Parameters

Combine splats and named parameters:

const route = Route('/books/*section/:title');

route.match('/books/some/section/last-words-a-memoir');
// => { section: 'some/section', title: 'last-words-a-memoir' }

Optional Segments

Make parts of the route optional with parentheses:

const route = Route('/users/:id(/style/:style)');

route.match('/users/3');
// => { id: '3', style: undefined }

route.match('/users/3/style/pirate');
// => { id: '3', style: 'pirate' }

Optional segments starting with a word:

const route = Route('/things/(option/:first)');

route.match('/things/option/bar');
// => { first: 'bar' }

Nested optional segments:

const route = Route('/users/:id(/style/:style(/more/:param))');

route.match('/users/3');
// => { id: '3', style: undefined, param: undefined }

route.match('/users/3/style/pirate');
// => { id: '3', style: 'pirate', param: undefined }

route.match('/users/3/style/pirate/more/things');
// => { id: '3', style: 'pirate', param: 'things' }

API

Route(spec: string)

Creates a new route parser. Can be called with or without new.

const route1 = Route('/users/:id');
const route2 = new Route('/users/:id');

Throws an error if spec is not provided.

route.match(path: string): object | false

Matches a path against the route pattern. Returns an object with the captured parameters on success, or false if the path doesn't match.

const route = Route('/users/:id');

route.match('/users/123');  // => { id: '123' }
route.match('/posts/123');  // => false

route.reverse(params?: object): string | false

Generates a path from the route pattern and provided parameters. Returns the path string on success, or false if required parameters are missing.

const route = Route('/users/:id/posts/:postId');

route.reverse({ id: '1', postId: '42' });
// => '/users/1/posts/42'

route.reverse({ id: '1' });
// => false (missing required parameter)

With optional segments:

const route = Route('/things/(option/:first)');

route.reverse({ first: 'bar' });
// => '/things/option/bar'

route.reverse();
// => '/things/' (optional segment omitted)

Falsy values (like 0) are handled correctly:

const route = Route('/items/:id?page=:page');

route.reverse({ id: 1, page: 0 });
// => '/items/1?page=0'

TypeScript

The library is written in TypeScript and includes type definitions.

import Route, { RouteParams } from 'route-parser-ts';

const route = Route('/users/:id');
const params: RouteParams | false = route.match('/users/123');

if (params) {
  console.log(params.id); // '123'
}

Acknowledgments

This library is a TypeScript reimplementation of route-parser by Ryan Sorensen. The original library provides the same functionality in JavaScript.

License

MIT