Skip to content

Commit 8d758be

Browse files
authored
1. make 3rd param of exec() optional 2. add types + jsdoc (#59)
1 parent e64caf8 commit 8d758be

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/router.d.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,27 @@ export function LocationProvider(props: {
77

88
type NestedArray<T> = Array<T | NestedArray<T>>;
99

10+
/**
11+
* Check if a URL path matches against a URL path pattern.
12+
*
13+
* Warning: This is an internal API exported only for testing purpose. API could change in future.
14+
* @param url - URL path (e.g. /user/12345)
15+
* @param route - URL pattern (e.g. /user/:id)
16+
*/
17+
export function exec(url: string, route: string, matches: {
18+
params: {
19+
[param: string]: string;
20+
};
21+
rest?: string;
22+
[props: string]: string;
23+
}): {
24+
params: {
25+
[param: string]: string;
26+
},
27+
rest?: string;
28+
[propsOrParam: string]: string;
29+
}
30+
1031
export function Router(props: {
1132
onRouteChange?: (url: string) => void;
1233
onLoadEnd?: (url: string) => void;

src/router.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ const UPDATE = (state, url) => {
4848
return url;
4949
};
5050

51-
export const exec = (url, route, matches) => {
51+
export const exec = (url, route, matches = {}) => {
5252
url = url.split('/').filter(Boolean);
5353
route = (route || '').split('/').filter(Boolean);
54+
if (!matches.params) matches.params = {};
5455
for (let i = 0, val, rest; i < Math.max(url.length, route.length); i++) {
5556
let [, m, param, flag] = (route[i] || '').match(/^(:?)(.*?)([+*?]?)$/);
5657
val = url[i];

0 commit comments

Comments
 (0)