Skip to content

Commit

Permalink
Export matchRoute function
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya authored and molefrog committed Jun 17, 2024
1 parent 6abe383 commit d7305d0
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/wouter/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const useSearch = () => {
return unescape(stripQm(router.searchHook(router)));
};

const matchRoute = (parser, route, path, loose) => {
export const matchRoute = (parser, route, path, loose) => {
// if the input is a regexp, skip parsing
const { pattern, keys } =
route instanceof RegExp
Expand Down
55 changes: 55 additions & 0 deletions packages/wouter/test/match-route.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { it, expectTypeOf, assertType } from "vitest";
import { matchRoute, useRouter } from "wouter";

const { parser } = useRouter();

it("should only accept strings", () => {
// @ts-expect-error
assertType(matchRoute(parser, Symbol(), ""));
// @ts-expect-error
assertType(matchRoute(parser, undefined, ""));
assertType(matchRoute(parser, "/", ""));
});

it('has a boolean "match" result as a first returned value', () => {
const [match] = matchRoute(parser, "/", "");
expectTypeOf(match).toEqualTypeOf<boolean>();
});

it("returns null as parameters when there was no match", () => {
const [match, params] = matchRoute(parser, "/foo", "");

if (!match) {
expectTypeOf(params).toEqualTypeOf<null>();
}
});

it("accepts the type of parameters as a generic argument", () => {
const [match, params] = matchRoute<{ id: string; name: string | undefined }>(
parser,
"/app/users/:name?/:id",
""
);

if (match) {
expectTypeOf(params).toEqualTypeOf<{
id: string;
name: string | undefined;
}>();
}
});

it("infers parameters from the route path", () => {
const [, inferedParams] = matchRoute(parser, "/app/users/:name?/:id/*?", "");

if (inferedParams) {
expectTypeOf(inferedParams).toMatchTypeOf<{
0?: string;
1?: string;
2?: string;
name?: string;
id: string;
wildcard?: string;
}>();
}
});
22 changes: 21 additions & 1 deletion packages/wouter/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
BrowserSearchHook,
} from "./use-browser-location.js";

import { RouterObject, RouterOptions } from "./router.js";
import { Parser, RouterObject, RouterOptions } from "./router.js";

// these files only export types, so we can re-export them as-is
// in TS 5.0 we'll be able to use `export type * from ...`
Expand Down Expand Up @@ -191,4 +191,24 @@ export function useParams<T = undefined>(): T extends string
? DefaultParams
: T;

/*
* Helpers
*/

export function matchRoute<
T extends DefaultParams | undefined = undefined,
RoutePath extends PathPattern = PathPattern
>(
parser: Parser,
pattern: RoutePath,
path: string,
loose?: boolean
): Match<
T extends DefaultParams
? T
: RoutePath extends string
? StringRouteParams<RoutePath>
: RegexRouteParams
>;

// tslint:enable:no-unnecessary-generics

0 comments on commit d7305d0

Please sign in to comment.