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

Added support for regular expressions in paths #450

Merged
merged 7 commits into from
May 30, 2024

Conversation

JonahPlusPlus
Copy link
Contributor

@JonahPlusPlus JonahPlusPlus commented May 28, 2024

This fixes #449.

By supporting regular expressions in paths, it becomes possible to lift validation out of routes and into the paths themselves.

What happens when you use a RegExp:
The route doesn't run the parser, but just executes the regex. After executing the regex, instead of mapping the keys and values into an object, the indices of the regex result are mapped to an object and the named groups are assigned to the object as well.

For example:

// Route
/[/]([a-z]+)/
// Against "/foo", produces
{ 0: "foo" }

// Route
/[/](?<name>[a-z]+)/
// Against "/foo", produces
{ 0: "foo", name: "foo" }

Another thing to consider: should normal paths also expose params as indices? (so, should /:id become { 0: "foo", id: "foo" }, instead of just { id: "foo" }?)

Copy link

stackblitz bot commented May 28, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Copy link

codecov bot commented May 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (e106a9d) to head (44181fb).

Additional details and impacted files
@@            Coverage Diff            @@
##                v3      #450   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines            1         1           
=========================================
  Hits             1         1           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@JonahPlusPlus JonahPlusPlus marked this pull request as ready for review May 28, 2024 19:16
@JonahPlusPlus JonahPlusPlus marked this pull request as draft May 28, 2024 19:21
@JonahPlusPlus
Copy link
Contributor Author

I changed it to handle the RegExp outside of the parser, so devs don't need to handle that case. With that, these changes aren't breaking (AFAIK; it's still a change to the public API).

@JonahPlusPlus JonahPlusPlus marked this pull request as ready for review May 28, 2024 19:34
@molefrog
Copy link
Owner

@JonahPlusPlus Hey, thank you! 👏 You've done an enormous work and everything seems good. I've looked briefly, so I'm going to take some time to understand better how it works.

README.md Outdated Show resolved Hide resolved
@JonahPlusPlus
Copy link
Contributor Author

JonahPlusPlus commented May 29, 2024

Okay, so I got those changes done and I added one final change: string paths also return indices for the params as well as the keys.

So, matching "/:id" against /foo returns { 0: "foo", id: "foo" }.
This is now consistent with regex paths.
Keys override indices, so this should not be a breaking change for any library that uses number keys.
(e.g. something that acts like "/:1/:0" won't break order)

Future work:
Provide a way to optimize routes by caching string path regexes.
Possible solution: allow passing { pattern, keys } objects to routes and allow for generating those objects outside of routes.

const idPath = cachePath("/:id");

<Route path={idPath}>
...
</Route>

Copy link
Owner

@molefrog molefrog left a comment

Choose a reason for hiding this comment

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

Amazing job! I have no more comments 😃

@molefrog molefrog merged commit af81d14 into molefrog:v3 May 30, 2024
4 checks passed
@JonahPlusPlus JonahPlusPlus deleted the regexp branch June 1, 2024 03:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow using RegExp for paths alongside strings
2 participants