Skip to content

Commit

Permalink
fix(runtime-utils): the normalizePathname should handles multiple sla…
Browse files Browse the repository at this point in the history
…shes correctly (#6441)
  • Loading branch information
yimingjfe authored Oct 25, 2024
1 parent 0e612b7 commit c4894e6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/weak-singers-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modern-js/runtime-utils': patch
---

fix(runtime-utils): the normalizePathname should handles multiple slashes
fix(runtime-utils): normalizePathname 函数应该处理好多个 / 的情况
13 changes: 8 additions & 5 deletions packages/toolkit/runtime-utils/src/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
*
* pathname3: '/', the nomalizeResult also as '/'
*/
export function normalizePathname(pathname: string) {
if (pathname === '/') {
return pathname;
} else {
return pathname.replace(/\/+$/, '');
export function normalizePathname(pathname: string): string {
// biome-ignore lint/style/useTemplate: <explanation>
const normalized = '/' + pathname.replace(/^\/+|\/+$/g, '');

if (normalized === '/') {
return normalized;
}

return normalized;
}
6 changes: 6 additions & 0 deletions packages/toolkit/runtime-utils/tests/url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,11 @@ describe('test ./src/url.ts', () => {

pathname = '/a/b/c/d/';
expect(normalizePathname(pathname)).toBe('/a/b/c/d');

pathname = '//';
expect(normalizePathname(pathname)).toBe('/');

pathname = '/api//';
expect(normalizePathname(pathname)).toBe('/api');
});
});
2 changes: 1 addition & 1 deletion tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"main": "index.js",
"scripts": {
"test:rspack": "pnpm test:builder:rspack && pnpm test:framework && pnpm test:garfish:rspack",
"test:framework": "MODERN_SERVER_LOG_LEVEL=info jest",
"test:framework": "cross-env MODERN_SERVER_LOG_LEVEL=info jest",
"test:builder:rspack": "cd e2e/builder && pnpm test:rspack",
"test:garfish:rspack": "cd e2e/garfish && pnpm test:rspack",
"test:module-tools": "cd integration && jest --testMatch **/module/**/*.test.ts",
Expand Down

0 comments on commit c4894e6

Please sign in to comment.