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

perf(shared): avoid using regexp match #1315

Merged
merged 2 commits into from May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion packages/shared/src/utils/ensureEndingSlash.ts
Expand Up @@ -2,4 +2,4 @@
* Ensure a url string to have ending slash /
*/
export const ensureEndingSlash = (str: string): string =>
/(\.html|\/)$/.test(str) ? str : str + '/'
str[str.length - 1] === '/' || str.endsWith('.html') ? str : `${str}/`
2 changes: 1 addition & 1 deletion packages/shared/src/utils/ensureLeadingSlash.ts
Expand Up @@ -2,4 +2,4 @@
* Ensure a url string to have leading slash /
*/
export const ensureLeadingSlash = (str: string): string =>
str.replace(/^\/?/, '/')
str[0] === '/' ? str : `/${str}`
3 changes: 2 additions & 1 deletion packages/shared/src/utils/removeEndingSlash.ts
@@ -1,4 +1,5 @@
/**
* Remove ending slash / from a string
*/
export const removeEndingSlash = (str: string): string => str.replace(/\/$/, '')
export const removeEndingSlash = (str: string): string =>
str[str.length - 1] === '/' ? str.slice(0, -1) : str
2 changes: 1 addition & 1 deletion packages/shared/src/utils/removeLeadingSlash.ts
Expand Up @@ -2,4 +2,4 @@
* Remove leading slash / from a string
*/
export const removeLeadingSlash = (str: string): string =>
str.replace(/^\//, '')
str[0] === '/' ? str.slice(1) : str
12 changes: 8 additions & 4 deletions packages/shared/src/utils/resolveRoutePathFromUrl.ts
@@ -1,6 +1,10 @@
export const resolveRoutePathFromUrl = (url: string, base = '/'): string =>
url
export const resolveRoutePathFromUrl = (url: string, base = '/'): string => {
const pathname = url
// remove url origin
.replace(/^(https?:)?\/\/[^/]*/, '')
// remove site base
.replace(new RegExp(`^${base}`), '/')

// remove site base
return pathname.startsWith(base)
? `/${pathname.slice(base.length)}`
: pathname
}