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

pathic

v0.1.4

Published

TypeScript library for URI path matching

Downloads

403

Readme

pathic

TypeScript library for URI path matching. Focused on a blend of efficiency and simplicity.

Features:

  • InferParams type for deriving an object type from a path pattern.
  • PathTemplate type for creating a template literal type from a path pattern.
  • compilePaths for compiling an array of paths into a function that efficiently matches a path to an index in the array. Paths are sorted by specificity and parameters are parsed.
  • buildPath for turning a path pattern and an object of parameters into a string.
  • parsePathParams for returning an array of parameter names in the path.

Path patterns

  • Fixed routes /foo
    Only matches /foo and takes precedence over all other patterns.

  • Named parameters /foo/:bar
    Matches /foo/123 but not /foo/123/456.

  • Catch-all parameters /foo/*bar
    Matches any path starting with /foo/ but not /foo.

  • Unnamed catch-all parameters /foo/*
    Matches any path starting with /foo/ but the tail isn't included in the parsed parameter values. Only one unnamed catch-all parameter is allowed.

 

API

parsePathParams

Returns an array of parameter names in the path.

parsePathParams('/foo/:bar/*baz') // => ['bar', 'baz']

compilePaths

Compiles an array of paths into a function that matches a path to an index in the array. This function is order-independent, since it internally sorts the paths by specificity.

If your callback returns anything other than undefined, matching stops and the result is returned. Otherwise, the next matching path is tried.

const routes = [
  ['/foo', () => 1],
  ['/foo/:bar', ({ bar }) => [2, bar]],
  ['/foo/*bar', ({ bar }) => [3, bar]],
  ['/foo/*bar/baz', ({ bar }) => [4, bar]],
]

const match = compilePaths(routes.map(([path]) => path))

const handle = (index: number, params: Record<string, string>) => {
  const [, handler] = routes[index]
  return handler(params)
}

match('/foo', handle) // => 1
match('/foo/bar', handle) // => [2, 'bar']
match('/foo/bar/baz', handle) // => [4, 'bar']
match('/foo/bar/bie', handle) // => [3, 'bar/bie']
match('/404', handle) // => undefined

buildPath

Turns a path and a params object into a string. If a parameter is an array, it is joined with slashes. If a parameter is missing, an error is thrown. Otherwise, the parameter is stringified.

buildPath('/foo/:bar', { bar: 'baz' }) // => '/foo/baz'
buildPath('/foo/:bar', { bar: ['bar', 'bie'] }) // => '/foo/bar/bie'
buildPath('/foo/:bar', { bar: undefined }) // => throws Error

 

Types

InferParams

Derive an object type from a path pattern.

InferParamsArray

Derive a tuple type, containing parameter values from a path pattern.

InferParamNames

Derive a tuple type, containing parameter names from a path pattern.

PathTemplate

Create a template literal type from a path pattern.