Skip to content

Commit 31aa54d

Browse files
authored
Add a location param to Router.getRoute (#53)
1 parent e17f6f4 commit 31aa54d

File tree

5 files changed

+24
-6
lines changed

5 files changed

+24
-6
lines changed

__tests__/router.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,18 @@ test("getRoute: should match the correct route", () => {
9696
expect(container.textContent).toContain("Not found");
9797
});
9898

99+
test("getRoute: should match the correct route for given location", () => {
100+
const route = getRoute(routesToMatch, "/profile/zoontek");
101+
102+
expect(route).toStrictEqual({
103+
key: "1pkttpl-0",
104+
name: "Profile",
105+
params: {
106+
username: "zoontek",
107+
},
108+
});
109+
});
110+
99111
test("useFocusReset: should focus the correct element", () => {
100112
const App = () => {
101113
const route = useRoute(routesToMatch);

docs/docs/router.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,16 @@ const route = Router.useRoute(["Home", "UserArea"]);
3737
Takes an array of routes (a subset of the router), and returns the route and its params if one matches.
3838

3939
- `routes` (**required**): `RouteName[]`
40+
- `location` (optional): `string` (defaults to the current browser location)
4041

4142
Returns a route match (or `undefined` if nothing matches):
4243

4344
- `name`: a route name
4445
- `params`: its associated params
4546

4647
```ts
47-
const route = Router.getRoute(["Home", "UserArea"]);
48+
const routeA = Router.getRoute(["Home", "UserArea"]);
49+
const routeB = Router.getRoute(["User"], "/users/1");
4850
```
4951

5052
## Router.push

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@swan-io/chicane",
3-
"version": "2.1.0",
3+
"version": "2.2.0",
44
"license": "MIT",
55
"description": "A simple and safe router for React and TypeScript",
66
"author": "Mathieu Acthernoene <[email protected]>",

src/createRouter.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useSyncExternalStoreWithSelector } from "use-sync-external-store/shim/w
33
import { concatRoutes } from "./concatRoutes";
44
import { areRouteEqual, first, identity } from "./helpers";
55
import {
6+
decodeLocation,
67
getLocation,
78
parseRoute,
89
pushUnsafe,
@@ -123,18 +124,21 @@ export const createRouter = <
123124

124125
const getRoute = <RouteName extends keyof FiniteRoutes | keyof AreaRoutes>(
125126
routeNames: ReadonlyArray<RouteName>,
127+
location?: string,
126128
): RouteName extends string
127129
?
128130
| { key: string; name: RouteName; params: RoutesParams[RouteName] }
129131
| undefined
130132
: never => {
131-
const location = getLocation();
133+
const locationObject =
134+
location != null ? decodeLocation(location) : getLocation();
135+
132136
const matchers = rankedMatchers.filter(({ name }) =>
133137
routeNames.includes(name as RouteName),
134138
);
135139

136140
// @ts-expect-error
137-
return match(location, matchers);
141+
return match(locationObject, matchers);
138142
};
139143

140144
const push = <RouteName extends keyof FiniteRoutes>(

src/history.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ export const parseRoute = (route: string): RouteObject => {
3030
};
3131
};
3232

33-
export const decodeLocation = (url: string): Location => {
34-
const route = parseRoute(url);
33+
export const decodeLocation = (input: string): Location => {
34+
const route = parseRoute(input);
3535
const path = route.path.substring(1);
3636

3737
const parsedPath =

0 commit comments

Comments
 (0)