Skip to content

Commit 9bb4505

Browse files
authored
Merge pull request #8 from strblr/refactor-weight-computation
refactor: Update route weight handling
2 parents 0aad680 + f233649 commit 9bb4505

5 files changed

Lines changed: 14 additions & 33 deletions

File tree

packages/devtools/src/components/panel.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ function DetailsSection({ currentMatch, selectedRoute }: DetailsSectionProps) {
214214
</div>
215215
<InfoSection label="Pattern meta" defaultExpanded={false}>
216216
<Inspector
217-
value={{ keys: route._.keys, weights: route._.weights }}
217+
value={{ keys: route._.keys, weight: route._.weight }}
218218
/>
219219
</InfoSection>
220220
<div style={styles.infoRow()}>

packages/router/src/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class Route<
4646
keys: string[];
4747
regex: RegExp;
4848
loose: RegExp;
49-
weights: number[];
49+
weight: string;
5050
validate: (search: Record<string, unknown>) => S;
5151
handles: Handle[];
5252
components: ComponentType[];

packages/router/src/router/router.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
import { inject } from "regexparam";
22
import { BrowserHistory } from "./browser-history";
33
import type { Route } from "../route";
4-
import {
5-
normalizePath,
6-
mergeUrl,
7-
match,
8-
rankMatches,
9-
absolutePath
10-
} from "../utils";
4+
import { normalizePath, mergeUrl, match, absolutePath } from "../utils";
115
import type {
126
NavigableRoute,
137
RouterOptions,
@@ -75,10 +69,13 @@ export class Router {
7569
};
7670

7771
matchAll = (path: string): Match | null => {
78-
const matches = this.routes
79-
.map(route => this.match(path, { from: route, strict: true }))
80-
.filter(m => !!m);
81-
return rankMatches(matches)[0] ?? null;
72+
return (
73+
this.routes
74+
.map(route => this.match(path, { from: route, strict: true }))
75+
.filter(m => !!m)
76+
.sort((a, b) => b.route._.weight.localeCompare(a.route._.weight))[0] ??
77+
null
78+
);
8279
};
8380

8481
createUrl = <P extends Pattern>(options: NavigateOptions<P>) => {

packages/router/src/utils/route.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ export const normalizePath = <P extends string>(path: P) => {
1111
export const parsePattern = <P extends string>(pattern: P) => {
1212
const { keys, pattern: regex } = parse(pattern);
1313
const loose = parse(pattern, true).pattern;
14-
const weights = pattern
14+
const weight = pattern
1515
.split("/")
1616
.slice(1)
17-
.map(s => (s.includes("*") ? 0 : s.includes(":") ? 1 : 2));
18-
return { pattern, keys, regex, loose, weights };
17+
.map(s => (s.includes("*") ? 0 : s.includes(":") ? 1 : 2))
18+
.join("");
19+
return { pattern, keys, regex, loose, weight };
1920
};
2021

2122
export const validator = <Input extends {}, Output extends {}>(

packages/router/src/utils/router.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { normalizePath } from "./route";
22
import { parseSearch, stringifySearch } from "./search";
33
import type { Route } from "../route";
4-
import type { Match } from "../types";
54

65
export const absolutePath = (rpath: string, basePath: string) => {
76
return normalizePath(`${basePath}/${rpath}`);
@@ -38,19 +37,3 @@ export const match = (
3837
});
3938
return out;
4039
};
41-
42-
export const rankMatches = (matches: Match[]) => {
43-
return [...matches].sort((a, b) => {
44-
const was = a.route._.weights;
45-
const wbs = b.route._.weights;
46-
const length = Math.max(was.length, wbs.length);
47-
for (let i = 0; i < length; i++) {
48-
const wa = was[i] ?? -1;
49-
const wb = wbs[i] ?? -1;
50-
if (wa !== wb) {
51-
return wb - wa;
52-
}
53-
}
54-
return 0;
55-
});
56-
};

0 commit comments

Comments
 (0)