Skip to content

Commit

Permalink
feat: reduce number of regex operations for normalizing paths
Browse files Browse the repository at this point in the history
  • Loading branch information
loilo committed Oct 13, 2023
1 parent babcc16 commit 2b225be
Showing 1 changed file with 6 additions and 17 deletions.
23 changes: 6 additions & 17 deletions src/drivers/utils/opfs-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@ function ignoreNotfoundError(error: any): null {
* Normalize a path, removing empty segments and leading/trailing slashes
*/
export function normalizePath(path: string): string {
const normalizedWrappedPath = `/${path}/`
// Replace colons with slashes
.replace(/:/g, "/")

// Remove . segments
.replace(/\/\.\//g, "/")

// Remove duplicate slashes
.replace(/\/{2,}/g, "/");
// Wrap path in slashes, remove . segments and collapse subsequent namespace separators
const normalizedWrappedPath = `/${path}/`.replace(
/[/:]+(\.[/:]+)*[/:]*/g,
"/"
);

// Disallow .. segments
if (normalizedWrappedPath.includes("/../")) {
Expand All @@ -29,14 +25,7 @@ export function normalizePath(path: string): string {
);
}

return (
normalizedWrappedPath
// Remove leading slashes
.replace(/^\//g, "")

// Remove trailing slashes
.replace(/\/$/g, "")
);
return normalizedWrappedPath.slice(1, -1);
}

/**
Expand Down

0 comments on commit 2b225be

Please sign in to comment.