rsnext/packages/next/next-server/lib/router/utils/route-matcher.ts
Joe Haddad ba5e6943fa
[Experimental] Catch-all Route Support (#9416)
* Catch-all Route Support

* Add SSR tests

* Add additional test

* Add unit tests

* Put the feature behind a flag

* Apply suggestions from code review

Co-Authored-By: JJ Kasper <jj@jjsweb.site>

* Fix test
2019-11-14 23:19:41 -08:00

24 lines
697 B
TypeScript

import { getRouteRegex } from './route-regex'
export function getRouteMatcher(routeRegex: ReturnType<typeof getRouteRegex>) {
const { re, groups } = routeRegex
return (pathname: string | undefined) => {
const routeMatch = re.exec(pathname!)
if (!routeMatch) {
return false
}
const params: { [paramName: string]: string | string[] } = {}
Object.keys(groups).forEach((slugName: string) => {
const m = routeMatch[groups[slugName]]
if (m !== undefined) {
params[slugName] =
m.indexOf('/') !== -1
? m.split('/').map(entry => decodeURIComponent(entry))
: decodeURIComponent(m)
}
})
return params
}
}