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

Why is pathToRegexp mutating keys parameter? #236

Open
mihkeleidast opened this issue Dec 22, 2020 · 3 comments
Open

Why is pathToRegexp mutating keys parameter? #236

mihkeleidast opened this issue Dec 22, 2020 · 3 comments

Comments

@mihkeleidast
Copy link

mihkeleidast commented Dec 22, 2020

The examples are unclear on why it works that way.
Mutating params feels wrong, because in the code it is not very easy to understand how my empty keys array gets populated. So it's always necessary to add a comment about how it works (if I want the next developer to understand it as well).

Wouldn't it make better sense to return the keys from pathToRegexp? For example, like so:

type pathToRegexp = (path, opts) => { matcher: Regexp, keys: Keys };
@blakeembrey
Copy link
Member

blakeembrey commented Dec 22, 2020

This is just the API that was originally designed, and I’ve avoided changing it so far. I’m not sure there’s a good reason to change it. I’ve instead moved some functionality away from raw regexes to make it a bit more user friendly. We could probably start returning keys as a property of the RegExp to continue maintaining backward compatibility.

@mihkeleidast
Copy link
Author

mihkeleidast commented Dec 22, 2020

Immutability and clarity in code would be pretty good reasons in my opinion :)

I believe keys was a property of the RegExp sometime in the past. I had to figure out what happened to the keys in frctl/fractal#714 when I updated path-to-regexp from 1.7.0 to latest, hence the confusion and this issue. So reintroducing that solution would make sense to me, although I would prefer having a "clean" Regexp and a separate keys value since types get complicated when putting additional properties to native types.

@nickyu42
Copy link

nickyu42 commented Mar 8, 2022

Hi! I'm new to the project but I wanted to contribute to it since I really like it. I was wondering if there is any interest in this issue since I wanted to try to pick it up.

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

No branches or pull requests

3 participants