Skip to content

Commit 2ce838b

Browse files
authored
refactor: Move Redirector and others outside kernel (#124)
1 parent 86edd79 commit 2ce838b

File tree

12 files changed

+61
-39
lines changed

12 files changed

+61
-39
lines changed

src/lib/kernel/Redirector.svelte.test.ts renamed to src/lib/Redirector.svelte.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { testWithEffect as test } from "$test/testWithEffect.svelte.js";
33
import { ALL_HASHES, ROUTING_UNIVERSES } from "$test/test-utils.js";
44
import { init } from "$lib/init.js";
55
import type { Hash, PatternRouteInfo, RedirectedRouteInfo } from "$lib/types.js";
6-
import { resolveHashValue } from "./resolveHashValue.js";
6+
import { resolveHashValue } from "./kernel/resolveHashValue.js";
77
import { Redirector } from "./Redirector.svelte.js";
8-
import { location } from "./Location.js";
8+
import { location } from "./kernel/Location.js";
99
import { flushSync } from "svelte";
1010

1111
ROUTING_UNIVERSES.forEach((universe) => {

src/lib/kernel/Redirector.svelte.ts renamed to src/lib/Redirector.svelte.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Hash, ParameterValue, RedirectedRouteInfo } from "$lib/types.js";
2-
import { RouteHelper } from "./RouteHelper.svelte.js";
3-
import { location } from "./Location.js";
4-
import { resolveHashValue } from "./resolveHashValue.js";
2+
import { RouteHelper } from "./kernel/RouteHelper.svelte.js";
3+
import { location } from "./kernel/Location.js";
4+
import { resolveHashValue } from "./kernel/resolveHashValue.js";
55
import { untrack } from "svelte";
66

77
/**
@@ -124,7 +124,7 @@ export class Redirector {
124124
const url = typeof redirection.href === 'function' ?
125125
redirection.href(routeParams) :
126126
redirection.href;
127-
location[(redirection.goTo ? 'goTo' : 'navigate')](url, {
127+
location[redirection.goTo ? 'goTo' : 'navigate'](url, {
128128
hash: this.#hash,
129129
replace: this.#options.replace,
130130
...redirection.options,

src/lib/index.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ describe('index', () => {
2121
'activeBehavior',
2222
'Redirector',
2323
'buildHref',
24+
'joinPaths',
2425
];
2526

2627
// Act.

src/lib/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ export * from './RouterTrace/RouterTrace.svelte';
1414
export { default as RouterTrace } from './RouterTrace/RouterTrace.svelte';
1515
export * from "./public-utils.js";
1616
export * from "./behaviors/active.svelte.js";
17-
export { Redirector } from "./kernel/Redirector.svelte.js";
17+
export { Redirector } from "./Redirector.svelte.js";
1818
export { buildHref } from "./buildHref.js";

src/lib/kernel/RouteHelper.svelte.ts

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,8 @@
1+
import { joinPaths } from "$lib/public-utils.js";
12
import type { AndUntyped, Hash, PatternRouteInfo, RouteStatus } from "$lib/types.js";
3+
import { noTrailingSlash } from "$lib/utils.js";
24
import { location } from "./Location.js";
35

4-
function noTrailingSlash(path: string) {
5-
return path !== '/' && path.endsWith('/') ? path.slice(0, -1) : path;
6-
}
7-
8-
function hasLeadingSlash(paths: (string | undefined)[]) {
9-
for (let path of paths) {
10-
if (!path) {
11-
continue;
12-
}
13-
return path.startsWith('/');
14-
}
15-
return false;
16-
}
17-
18-
/**
19-
* Joins the provided paths into a single path.
20-
* @param paths Paths to join.
21-
* @returns The joined path.
22-
*/
23-
export function joinPaths(...paths: string[]) {
24-
const result = paths.reduce((acc, path, index) => {
25-
const trimmedPath = (path ?? '').replace(/^\/|\/$/g, '');
26-
return acc + (index > 0 && !acc.endsWith('/') && trimmedPath.length > 0 ? '/' : '') + trimmedPath;
27-
}, hasLeadingSlash(paths) ? '/' : '');
28-
return noTrailingSlash(result);
29-
}
30-
316
function escapeRegExp(string: string): string {
327
return string.replace(/[.+^${}()|[\]\\]/g, '\\$&');
338
}

src/lib/kernel/RouterEngine.svelte.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { location } from "./Location.js";
44
import { routingOptions } from "./options.js";
55
import { resolveHashValue } from "./resolveHashValue.js";
66
import { assertAllowedRoutingMode } from "$lib/utils.js";
7-
import { joinPaths, RouteHelper } from "./RouteHelper.svelte.js";
7+
import { RouteHelper } from "./RouteHelper.svelte.js";
8+
import { joinPaths } from "$lib/public-utils.js";
89

910
/**
1011
* RouterEngine's options.

src/lib/kernel/calculateHref.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Hash, PreserveQuery } from "../types.js";
22
import { dissectHrefs } from "./dissectHrefs.js";
33
import { location } from "./Location.js";
44
import { mergeQueryParams } from "./preserveQuery.js";
5-
import { joinPaths } from "./RouteHelper.svelte.js";
5+
import { joinPaths } from "$lib/public-utils.js";
66
import { resolveHashValue } from "./resolveHashValue.js";
77
import { calculateMultiHashFragment } from "./calculateMultiHashFragment.js";
88

src/lib/kernel/index.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ describe('index', () => {
66
const expectedList = [
77
'location',
88
'RouterEngine',
9-
'joinPaths',
109
'isConformantState',
1110
'calculateHref',
1211
'calculateState',

src/lib/kernel/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export { location } from "./Location.js";
22
export { RouterEngine } from "./RouterEngine.svelte.js";
3-
export { joinPaths } from "./RouteHelper.svelte.js";
43
export { isConformantState } from "./isConformantState.js";
54
export { calculateHref } from "./calculateHref.js";
65
export { calculateMultiHashFragment } from "./calculateMultiHashFragment.js";

src/lib/public-utils.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { RouterEngine } from "./kernel/RouterEngine.svelte.js";
22
import type { RouteStatus } from "./types.js";
3+
import { noTrailingSlash } from "./utils.js";
34

45
/**
56
* Checks if a specific route is active according to the provided router engine or route status record.
@@ -16,3 +17,26 @@ export function isRouteActive(
1617
const rs = rsOrRouter instanceof RouterEngine ? rsOrRouter.routeStatus : rsOrRouter;
1718
return !!rs?.[key ?? '']?.match;
1819
}
20+
21+
function hasLeadingSlash(paths: (string | undefined)[]) {
22+
for (let path of paths) {
23+
if (!path) {
24+
continue;
25+
}
26+
return path.startsWith('/');
27+
}
28+
return false;
29+
}
30+
31+
/**
32+
* Joins the provided paths into a single path.
33+
* @param paths Paths to join.
34+
* @returns The joined path.
35+
*/
36+
export function joinPaths(...paths: string[]) {
37+
const result = paths.reduce((acc, path, index) => {
38+
const trimmedPath = (path ?? '').replace(/^\/|\/$/g, '');
39+
return acc + (index > 0 && !acc.endsWith('/') && trimmedPath.length > 0 ? '/' : '') + trimmedPath;
40+
}, hasLeadingSlash(paths) ? '/' : '');
41+
return noTrailingSlash(result);
42+
}

0 commit comments

Comments
 (0)