A robust and light-weight path-to-regexp alternative
MIT License
This module is entirely extracted from vue-router, it's basically a light-weight version of path-to-regexp with path ranking support.
npm i @egoist/path-parser
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' ]
The code is extracted from vue-router, all credits to its author @posva. The code might differ from the upstream in the future.
MIT © EGOIST