route.ts 565 B

1234567891011121314151617181920
  1. export const fillRoutePath = <T extends string>(path: T, params: ExtractRouteParams<T>) => {
  2. let processPath: string = path
  3. for (const [key, value] of Object.entries(params)) {
  4. const rg = new RegExp(`:${key}`)
  5. processPath = processPath.replace(rg, value as string)
  6. }
  7. return processPath
  8. }
  9. const place = /(?:\/:([^/]*))/g
  10. export const verifiRoutePath = (pathname: string, path: string) => {
  11. const rg = path
  12. .replace(/\/([^:])/g, (_, d) => `\\/${d}`)
  13. .replace(place, () => `(?:/([^/]*))`)
  14. return new RegExp(`^${rg}$`).exec(pathname)
  15. }