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
12 changes: 12 additions & 0 deletions __tests__/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ test("getRoute: should match the correct route", () => {
expect(container.textContent).toContain("Not found");
});

test("getRoute: should match the correct route for given location", () => {
const route = getRoute(routesToMatch, "/profile/zoontek");

expect(route).toStrictEqual({
key: "1pkttpl-0",
name: "Profile",
params: {
username: "zoontek",
},
});
});

test("useFocusReset: should focus the correct element", () => {
const App = () => {
const route = useRoute(routesToMatch);
Expand Down
4 changes: 3 additions & 1 deletion docs/docs/router.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ const route = Router.useRoute(["Home", "UserArea"]);
Takes an array of routes (a subset of the router), and returns the route and its params if one matches.

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

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

- `name`: a route name
- `params`: its associated params

```ts
const route = Router.getRoute(["Home", "UserArea"]);
const routeA = Router.getRoute(["Home", "UserArea"]);
const routeB = Router.getRoute(["User"], "/users/1");
```

## Router.push
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swan-io/chicane",
"version": "2.1.0",
"version": "2.2.0",
"license": "MIT",
"description": "A simple and safe router for React and TypeScript",
"author": "Mathieu Acthernoene <[email protected]>",
Expand Down
8 changes: 6 additions & 2 deletions src/createRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useSyncExternalStoreWithSelector } from "use-sync-external-store/shim/w
import { concatRoutes } from "./concatRoutes";
import { areRouteEqual, first, identity } from "./helpers";
import {
decodeLocation,
getLocation,
parseRoute,
pushUnsafe,
Expand Down Expand Up @@ -123,18 +124,21 @@ export const createRouter = <

const getRoute = <RouteName extends keyof FiniteRoutes | keyof AreaRoutes>(
routeNames: ReadonlyArray<RouteName>,
location?: string,
): RouteName extends string
?
| { key: string; name: RouteName; params: RoutesParams[RouteName] }
| undefined
: never => {
const location = getLocation();
const locationObject =
location != null ? decodeLocation(location) : getLocation();

const matchers = rankedMatchers.filter(({ name }) =>
routeNames.includes(name as RouteName),
);

// @ts-expect-error
return match(location, matchers);
return match(locationObject, matchers);
};

const push = <RouteName extends keyof FiniteRoutes>(
Expand Down
4 changes: 2 additions & 2 deletions src/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export const parseRoute = (route: string): RouteObject => {
};
};

export const decodeLocation = (url: string): Location => {
const route = parseRoute(url);
export const decodeLocation = (input: string): Location => {
const route = parseRoute(input);
const path = route.path.substring(1);

const parsedPath =
Expand Down