Skip to content

Commit fab1317

Browse files
committed
feat: reduce number of regex operations for normalizing paths
1 parent babcc16 commit fab1317

File tree

1 file changed

+7
-18
lines changed

1 file changed

+7
-18
lines changed

src/drivers/utils/opfs-utils.ts

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,21 @@ function ignoreNotfoundError(error: any): null {
1111
* Normalize a path, removing empty segments and leading/trailing slashes
1212
*/
1313
export function normalizePath(path: string): string {
14-
const normalizedWrappedPath = `/${path}/`
15-
// Replace colons with slashes
16-
.replace(/:/g, "/")
17-
18-
// Remove . segments
19-
.replace(/\/\.\//g, "/")
20-
21-
// Remove duplicate slashes
22-
.replace(/\/{2,}/g, "/");
14+
// Wrap path in slashes, remove . segments and collapse subsequent namespace separators
15+
const normalizedWrappedPath = `/${path}/`.replace(
16+
/[/:]+(\.[/:]+)*[/:]*/g,
17+
"/"
18+
);
2319

24-
// Disallow .. segments
20+
// Prohibit .. segments
2521
if (normalizedWrappedPath.includes("/../")) {
2622
throw createError(
2723
DRIVER_NAME,
2824
`Invalid key: ${JSON.stringify(path)}. It must not contain .. segments`
2925
);
3026
}
3127

32-
return (
33-
normalizedWrappedPath
34-
// Remove leading slashes
35-
.replace(/^\//g, "")
36-
37-
// Remove trailing slashes
38-
.replace(/\/$/g, "")
39-
);
28+
return normalizedWrappedPath.slice(1, -1);
4029
}
4130

4231
/**

0 commit comments

Comments
 (0)