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

Expose the groups keys in patterns #147

Open
posva opened this issue Nov 10, 2021 · 2 comments
Open

Expose the groups keys in patterns #147

posva opened this issue Nov 10, 2021 · 2 comments
Labels
addition/proposal New features or enhancements

Comments

@posva
Copy link

posva commented Nov 10, 2021

When creating a pattern like new URLPattern('/:id/static/:tags*'), there is no way of knowing what groups the pattern has (id and tags) nor their nature (optional, repeatable).

I propose a way of getting these groups:

const pattern = new URLPattern('/:id/static/:tags*')

pattern.pathname.value // '/:id/static/:tags*'
pattern.pathname.groups // { id: { optional: false, repeatable: false }, tags: { optional: true, repeatable: true }}
// or maybe with flags
const GROUP_REPEATABLE = 1
const GROUP_OPTIONAL = 2
const GROUP_REPEATABLE_OPTIONAL = GROUP_REPEATABLE & GROUP_OPTIONAL
pattern.pathname.groups // { id: 0, tags: 3 }

It's also worth noting this can be typed in TS too with string literals.

@wanderview
Copy link
Member

Interesting idea, but I must admit I don't understand what this would be used for. Can you describe the use case? Thanks.

@posva
Copy link
Author

posva commented Nov 10, 2021

I would consider this useful for error reporting and inspecting purposes. For example, if a user tries to build a path by passing an object of groups to the pattern (which I believe must be another issue somewhere but doing pattern.buildURL({ id: 2 }) -> /2/static), we can check any missing params and give the user better debugging information.
It can also be used to build UI around URL builders or checkers. For instance, I rely on this on the Vue Devtools.

Overall, this information would be very useful to extend the URLPattern API and would avoid hacks with Regexps to parse existing groups (which would partially defeat the purpose of URLPattern)

@domenic domenic added addition/proposal New features or enhancements and removed enhancement labels Oct 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition/proposal New features or enhancements
Development

No branches or pull requests

3 participants