UNPKG

459 BPlain TextView Raw
1import toRegExp from 'regexparam'
2
3import { RouteParameters } from './types'
4
5// @see https://github.com/lukeed/regexparam#usage
6export function getRouteParams(url: string, route: string): RouteParameters {
7 const [path] = url.split('?')
8 const result = toRegExp(route)
9 let i = 0
10 let out: RouteParameters = {}
11 let matches = result.pattern.exec(path) || []
12
13 while (i < result.keys.length) {
14 out[result.keys[i]] = matches[++i]
15 }
16
17 return out
18}