Skip to content

Commit

Permalink
1. make 3rd param of exec() optional 2. add types + jsdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
Munawwar committed Jan 27, 2025
1 parent e64caf8 commit c67c7df
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
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

0 comments on commit c67c7df

Please sign in to comment.