my-way
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

my-way

npm version Coverage Status Install size License

Minimal path matcher.

Usage

import matchRoute from 'my-way'
 
const route = '/users/:id'
const matched = matchRoute(route, '/users/123') // => { id: "123" }

Segments

  • /literal Literal segment
  • /:name Named segment
  • /:name+ Rest segment
  • /:name? Optional segment
  • /:name* Optional rest segment
  • /:name<regex> Named segment with regex matching
  • /:name+<regex> Rest segment with regex matching
  • /:name?<regex> Optional segment with regex matching
  • /:name*<regex> Optional rest segment with regex matching

Examples

matchRoute('/users/:id', '/users')
// null
 
matchRoute('/users/:id', '/users/123')
// { id: "123" }
 
matchRoute('/users/:id', '/users/123/')
// { id: "123" }
 
matchRoute('/users/:id', '/users/123/?query=will-be-omitted')
// { id: "123" }
 
matchRoute('/users/:id<\\d+>', '/users/alice')
// null
 
matchRoute('/users/:id<\\d+>', '/users/123')
// { id: "123" }
 
matchRoute('/users/:id/:status?<active|inactive>', '/users/123')
// { id: "123" }
 
matchRoute('/users/:id/:status?<active|inactive>', '/users/123/active')
// { id: "123", status: "active" }
 
matchRoute('/users/:id/:status?<active|inactive>', '/users/123/blue')
// null
 
matchRoute('/:owner/:repo/:path+', '/amio/my-way/package.json')
// { owner: "amio", repo: "my-way", path: "package.json" }
 
matchRoute('/:owner/:repo/:path+', '/amio/my-way/src/index.ts')
// { owner: "amio", repo: "my-way", path: "src/index.ts" }
 
matchRoute('/:owner/:repo/:path+', '/amio/my-way')
// null
 
matchRoute('/:owner/:repo/:path*', '/amio/my-way')
// { owner: "amio", repo: "my-way" }
 
matchRoute('/:owner/:repo/:url*', '/amio/my-way/https://github.com/amio/my-way')
// { owner: "amio", repo: "my-way", url: "https://github.com/amio/my-way" }
 
matchRoute('/:owner/:repo/:url*', '/amio/my-way/https%3A%2F%2Fgithub.com%2Famio%2Fmy-way')
// { owner: "amio", repo: "my-way", url: "https://github.com/amio/my-way" }

Not supported

  • Multi slashes in pathname

    matchRoute('/:owner/:repo', '//my-way')
    // null

Package Sidebar

Install

npm i my-way

Weekly Downloads

32

Version

2.0.0

License

MIT

Unpacked Size

8.31 kB

Total Files

6

Last publish

Collaborators

  • amio