Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/lib/core/RouterEngine.svelte.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ describe("RouterEngine", () => {
// Assert.
expect(basePath).toBe('/parent/child');
});
test("Should preserve the trailing slash.", () => {
test("Should remove the trailing slash.", () => {
// Arrange.
const router = new RouterEngine();
router.basePath = '/abc/';
Expand All @@ -103,7 +103,7 @@ describe("RouterEngine", () => {
const basePath = router.basePath;

// Assert.
expect(basePath).toBe('/abc/');
expect(basePath).toBe('/abc');
});
});
describe('url', () => {
Expand Down
9 changes: 6 additions & 3 deletions src/lib/core/RouterEngine.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@ function isRouterEngine(obj: unknown): obj is RouterEngine {
*/
export function joinPaths(...paths: string[]) {
const hasLeadingSlash = paths[0].startsWith('/');
const hasTrailingSlash = paths[paths.length - 1].endsWith('/');
const result = paths.reduce((acc, path, index) => {
const trimmedPath = (path ?? '').replace(/^\/|\/$/g, '');
return acc + (index > 0 && !acc.endsWith('/') && trimmedPath.length > 0 ? '/' : '') + trimmedPath;
}, hasLeadingSlash ? '/' : '');
return result + (hasTrailingSlash && result !== '/' ? '/' : '');
return noTrailingSlash(result);
}

function noTrailingSlash(path: string) {
return path !== '/' && path.endsWith('/') ? path.slice(0, -1) : path;
}

function routeInfoIsRegexInfo(info: unknown): info is RegexRouteInfo {
Expand Down Expand Up @@ -132,7 +135,7 @@ export class RouterEngine {
return this.#routePatterns;
}

#testPath = $derived.by(() => this.#hashId ? (location.hashPaths[this.#hashId] || '/') :this.url.pathname);
#testPath = $derived.by(() => noTrailingSlash(this.#hashId ? (location.hashPaths[this.#hashId] || '/') : this.url.pathname));

#routeStatusData = $derived.by(() => {
const routeStatus = {} as Record<string, RouteStatus>;
Expand Down