Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: re-use existed escapeRegex #34470

Merged
merged 3 commits into from Feb 28, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 4 additions & 8 deletions packages/next/shared/lib/router/utils/route-regex.ts
@@ -1,15 +1,11 @@
import { escapeStringRegexp } from '../../escape-regexp'

interface Group {
pos: number
repeat: boolean
optional: boolean
}

// this isn't importing the escape-string-regex module
// to reduce bytes
function escapeRegex(str: string) {
return str.replace(/[|\\{}()[\]^$+*?.-]/g, '\\$&')
}

function parseParameter(param: string) {
const optional = param.startsWith('[') && param.endsWith(']')
if (optional) {
Expand All @@ -34,7 +30,7 @@ export function getParametrizedRoute(route: string) {
groups[key] = { pos: groupIndex++, repeat, optional }
return repeat ? (optional ? '(?:/(.+?))?' : '/(.+?)') : '/([^/]+?)'
} else {
return `/${escapeRegex(segment)}`
return `/${escapeStringRegexp(segment)}`
}
})
.join('')
Expand Down Expand Up @@ -92,7 +88,7 @@ export function getParametrizedRoute(route: string) {
: `/(?<${cleanedKey}>.+?)`
: `/(?<${cleanedKey}>[^/]+?)`
} else {
return `/${escapeRegex(segment)}`
return `/${escapeStringRegexp(segment)}`
}
})
.join('')
Expand Down