@egoist/path-parser
v1.0.6
Published
A robust and light-weight path-to-regexp alternative
Downloads
302
Readme
@egoist/path-parser
This module is entirely extracted from vue-router, it's basically a light-weight version of path-to-regexp with path ranking support.
Install
npm i @egoist/path-parser
Usage
Create a path parser:
import { createParser } from '@egoist/path-parser'
const parser = createParser('/user/:user')
parser.parse('/user/egoist')
//=> { user: 'egoist' }
// `null` if not matched
Sort paths by ranking:
import { comparePathParserScore, createParser } from '@egoist/path-parser'
const paths = ['/:user', '/about']
paths.sort((a, b) => {
return comparePathParserScore(createParser(a), createParser(b))
})
//=> [ '/about', '/:user' ]
Credits
The code is extracted from vue-router, all credits to its author @posva. The code might differ from the upstream in the future.
License
MIT © EGOIST