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
21 changes: 21 additions & 0 deletions src/router.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@ export function LocationProvider(props: {

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

/**
* Check if a URL path matches against a URL path pattern.
*
* Warning: This is an internal API exported only for testing purpose. API could change in future.
* @param url - URL path (e.g. /user/12345)
* @param route - URL pattern (e.g. /user/:id)
*/
export function exec(url: string, route: string, matches: {
params: {
[param: string]: string;
};
rest?: string;
[props: string]: string;
}): {
params: {
[param: string]: string;
},
rest?: string;
[propsOrParam: string]: string;
}

export function Router(props: {
onRouteChange?: (url: string) => void;
onLoadEnd?: (url: string) => void;
Expand Down
3 changes: 2 additions & 1 deletion src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ const UPDATE = (state, url) => {
return url;
};

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