path-tree-matcher
v1.0.3
Published
Build and match path trees with parameters, wildcards, and optional route segments.
Maintainers
Readme
path-tree-matcher
A tiny TypeScript path tree matcher for static routes, optional groups, named parameters, and wildcards.
Quick usage
import { PathTree } from "path-tree-matcher"
const tree = new PathTree()
// register patterns
tree.setPattern("/a/b{/d}/hello")
tree.setPattern("/a/b/:name/hello")
tree.setPattern("/a/b/*all")
// match a pathname
const matches = tree.match("/a/b/d/hello")
for (const { pattern, params } of matches) {
console.log(pattern)
console.log(params)
}What these methods do
setPattern(pattern, mapper)registers a route pattern into the treematch(pathname)returns all matching route results with parameter values
Example behaviors
/a/b{/d}/hellomatches both/a/b/helloand/a/b/d/hello:namecaptures a single named segment*allcaptures a wildcard path segment sequence
Path syntax
- static text:
/users - named parameter:
/:id - wildcard:
/*rest - optional group:
{/segment} - segment grouping:
/"group"-nextyields/,"group", and-next - escape syntax:
/"hello\\"world"yields/,"hello"world"(\\is removed)
Syntax notes
- Invalid consecutive params:
/users/:id:action- unable to split path into two variables:
id, andaction
- unable to split path into two variables:
- Repeated variable name:
/users/:id/follow/:id- params[":id"] === [value0, value1]
- Pedantic mode:
/users/action!==users/action!==users/action/!==/users/action/
