Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add back keys property to regex #272

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,13 @@ export function match<P extends object = object>(
return regexpToFunction<P>(re, keys, options);
}

export interface RegExpWithKeys extends RegExp {
/**
* Optional property that includes key metadata corresponding to this regex.
*/
keys?: Key[];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this required? A way to get around TypeScript complaining is to use Object.assign(regex, { keys }).

}

/**
* Create a path match function from `path-to-regexp` output.
*/
Expand Down Expand Up @@ -479,9 +486,14 @@ function arrayToRegexp(
paths: Array<string | RegExp>,
keys?: Key[],
options?: TokensToRegexpOptions & ParseOptions
): RegExp {
): RegExpWithKeys {
const parts = paths.map((path) => pathToRegexp(path, keys, options).source);
return new RegExp(`(?:${parts.join("|")})`, flags(options));
const re: RegExpWithKeys = new RegExp(
`(?:${parts.join("|")})`,
flags(options)
);
re.keys = keys;
return re;
}

/**
Expand Down Expand Up @@ -533,7 +545,7 @@ export function tokensToRegexp(
tokens: Token[],
keys?: Key[],
options: TokensToRegexpOptions = {}
) {
): RegExpWithKeys {
const {
strict = false,
start = true,
Expand Down Expand Up @@ -596,7 +608,10 @@ export function tokensToRegexp(
}
}

return new RegExp(route, flags(options));
const re: RegExpWithKeys = new RegExp(route, flags(options));
re.keys = keys;

return re;
}

/**
Expand Down