All files / src/utils createRoute.ts

100% Statements 10/10
100% Branches 0/0
100% Functions 6/6
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30  88x       4x       4x       81x       81x       79x   79x 79x 79x   79x    
export function removeTrailingSlashesAndMultiSlashes(str: string): string {
  return str.replace(/\/+$/, '').replace(/(?<=\/)\/+/, '')
}
 
export function addTrailingWildcard(str: string): string {
  return str.replace(/(\/?)\*/g, '($1.*)?')
}
 
export function replaceDot(str: string): string {
  return str.replace(/\.(?=[\w(])/, '\\.')
}
 
export function addPathnameMatchBeforeRoute(route: string): string {
  return `[\\/[A-Za-z0-9:._-]*${route}`
}
 
export function addEndingTrailingSlashToRoute(route: string): string {
  return `${route}\\/*`
}
 
export function createRoute(route: string): RegExp {
  let routeRegExp = route
  // routeRegExp = addTrailingWildcard(routeRegExp) // Can be uncommented if wildcard (*) is needed
  routeRegExp = removeTrailingSlashesAndMultiSlashes(routeRegExp)
  routeRegExp = addPathnameMatchBeforeRoute(routeRegExp)
  routeRegExp = addEndingTrailingSlashToRoute(routeRegExp)
  // routeRegExp = replaceDot(routeRegExp) // Can be uncommented if dot (.) is needed
  return RegExp(`^${routeRegExp}$`)
}