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
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { testWithEffect as test } from "$test/testWithEffect.svelte.js";
import { ALL_HASHES, ROUTING_UNIVERSES } from "$test/test-utils.js";
import { init } from "$lib/init.js";
import type { Hash, PatternRouteInfo, RedirectedRouteInfo } from "$lib/types.js";
import { resolveHashValue } from "./resolveHashValue.js";
import { resolveHashValue } from "./kernel/resolveHashValue.js";
import { Redirector } from "./Redirector.svelte.js";
import { location } from "./Location.js";
import { location } from "./kernel/Location.js";
import { flushSync } from "svelte";

ROUTING_UNIVERSES.forEach((universe) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Hash, ParameterValue, RedirectedRouteInfo } from "$lib/types.js";
import { RouteHelper } from "./RouteHelper.svelte.js";
import { location } from "./Location.js";
import { resolveHashValue } from "./resolveHashValue.js";
import { RouteHelper } from "./kernel/RouteHelper.svelte.js";
import { location } from "./kernel/Location.js";
import { resolveHashValue } from "./kernel/resolveHashValue.js";
import { untrack } from "svelte";

/**
Expand Down Expand Up @@ -124,7 +124,7 @@ export class Redirector {
const url = typeof redirection.href === 'function' ?
redirection.href(routeParams) :
redirection.href;
location[(redirection.goTo ? 'goTo' : 'navigate')](url, {
location[redirection.goTo ? 'goTo' : 'navigate'](url, {
hash: this.#hash,
replace: this.#options.replace,
...redirection.options,
Expand Down
1 change: 1 addition & 0 deletions src/lib/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('index', () => {
'activeBehavior',
'Redirector',
'buildHref',
'joinPaths',
];

// Act.
Expand Down
2 changes: 1 addition & 1 deletion src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ export * from './RouterTrace/RouterTrace.svelte';
export { default as RouterTrace } from './RouterTrace/RouterTrace.svelte';
export * from "./public-utils.js";
export * from "./behaviors/active.svelte.js";
export { Redirector } from "./kernel/Redirector.svelte.js";
export { Redirector } from "./Redirector.svelte.js";
export { buildHref } from "./buildHref.js";
29 changes: 2 additions & 27 deletions src/lib/kernel/RouteHelper.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,8 @@
import { joinPaths } from "$lib/public-utils.js";
import type { AndUntyped, Hash, PatternRouteInfo, RouteStatus } from "$lib/types.js";
import { noTrailingSlash } from "$lib/utils.js";
import { location } from "./Location.js";

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

function hasLeadingSlash(paths: (string | undefined)[]) {
for (let path of paths) {
if (!path) {
continue;
}
return path.startsWith('/');
}
return false;
}

/**
* Joins the provided paths into a single path.
* @param paths Paths to join.
* @returns The joined path.
*/
export function joinPaths(...paths: string[]) {
const result = paths.reduce((acc, path, index) => {
const trimmedPath = (path ?? '').replace(/^\/|\/$/g, '');
return acc + (index > 0 && !acc.endsWith('/') && trimmedPath.length > 0 ? '/' : '') + trimmedPath;
}, hasLeadingSlash(paths) ? '/' : '');
return noTrailingSlash(result);
}

function escapeRegExp(string: string): string {
return string.replace(/[.+^${}()|[\]\\]/g, '\\$&');
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/kernel/RouterEngine.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { location } from "./Location.js";
import { routingOptions } from "./options.js";
import { resolveHashValue } from "./resolveHashValue.js";
import { assertAllowedRoutingMode } from "$lib/utils.js";
import { joinPaths, RouteHelper } from "./RouteHelper.svelte.js";
import { RouteHelper } from "./RouteHelper.svelte.js";
import { joinPaths } from "$lib/public-utils.js";

/**
* RouterEngine's options.
Expand Down
2 changes: 1 addition & 1 deletion src/lib/kernel/calculateHref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Hash, PreserveQuery } from "../types.js";
import { dissectHrefs } from "./dissectHrefs.js";
import { location } from "./Location.js";
import { mergeQueryParams } from "./preserveQuery.js";
import { joinPaths } from "./RouteHelper.svelte.js";
import { joinPaths } from "$lib/public-utils.js";
import { resolveHashValue } from "./resolveHashValue.js";
import { calculateMultiHashFragment } from "./calculateMultiHashFragment.js";

Expand Down
1 change: 0 additions & 1 deletion src/lib/kernel/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ describe('index', () => {
const expectedList = [
'location',
'RouterEngine',
'joinPaths',
'isConformantState',
'calculateHref',
'calculateState',
Expand Down
1 change: 0 additions & 1 deletion src/lib/kernel/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export { location } from "./Location.js";
export { RouterEngine } from "./RouterEngine.svelte.js";
export { joinPaths } from "./RouteHelper.svelte.js";
export { isConformantState } from "./isConformantState.js";
export { calculateHref } from "./calculateHref.js";
export { calculateMultiHashFragment } from "./calculateMultiHashFragment.js";
Expand Down
24 changes: 24 additions & 0 deletions src/lib/public-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { RouterEngine } from "./kernel/RouterEngine.svelte.js";
import type { RouteStatus } from "./types.js";
import { noTrailingSlash } from "./utils.js";

/**
* Checks if a specific route is active according to the provided router engine or route status record.
Expand All @@ -16,3 +17,26 @@ export function isRouteActive(
const rs = rsOrRouter instanceof RouterEngine ? rsOrRouter.routeStatus : rsOrRouter;
return !!rs?.[key ?? '']?.match;
}

function hasLeadingSlash(paths: (string | undefined)[]) {
for (let path of paths) {
if (!path) {
continue;
}
return path.startsWith('/');
}
return false;
}

/**
* Joins the provided paths into a single path.
* @param paths Paths to join.
* @returns The joined path.
*/
export function joinPaths(...paths: string[]) {
const result = paths.reduce((acc, path, index) => {
const trimmedPath = (path ?? '').replace(/^\/|\/$/g, '');
return acc + (index > 0 && !acc.endsWith('/') && trimmedPath.length > 0 ? '/' : '') + trimmedPath;
}, hasLeadingSlash(paths) ? '/' : '');
return noTrailingSlash(result);
}
21 changes: 20 additions & 1 deletion src/lib/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { afterEach, describe, expect, test, vi } from "vitest";
import { assertAllowedRoutingMode, expandAriaAttributes } from "./utils.js";
import { assertAllowedRoutingMode, expandAriaAttributes, noTrailingSlash } from "./utils.js";
import { ALL_HASHES } from "$test/test-utils.js";
import { resetRoutingOptions, setRoutingOptions } from "./kernel/options.js";
import type { ActiveStateAriaAttributes, ExtendedRoutingOptions, Hash } from "./types.js";
Expand Down Expand Up @@ -73,3 +73,22 @@ describe("expandAriaAttributes", () => {
expect(result).toEqual(expected);
});
});

describe("noTrailingSlash", () => {
test.each<{
input: string;
expected: string;
}>([
{ input: '/path/', expected: '/path' },
{ input: '/path/to/resource/', expected: '/path/to/resource' },
{ input: '/path', expected: '/path' },
{ input: '/', expected: '/' },
{ input: '', expected: '' },
])("Should convert $input to $expected .", ({ input, expected }) => {
// Act.
const result = noTrailingSlash(input);

// Assert.
expect(result).toBe(expected);
});
});
4 changes: 4 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@ export function expandAriaAttributes(aria: ActiveStateAriaAttributes | undefined
}
return result;
}

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