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

Should we expose a combined groups result? #62

Open
wanderview opened this issue Jul 23, 2021 · 1 comment
Open

Should we expose a combined groups result? #62

wanderview opened this issue Jul 23, 2021 · 1 comment
Labels
addition/proposal New features or enhancements

Comments

@wanderview
Copy link
Member

Currently each component in a URLPattern produces a separate groups map. They are accessed like result.pathname.groups, result.hostname.groups, etc. This is a bit verbose and also requires the person accessing the groups to understand which named group the component is in.

We could instead try to expose a result.groups that combines all the matched values from all the components into one map.

The main issue is what to do for conflicting group names. For example, if both the hostname and pathname have :id groups. Or worse, what if both hostname and pathname have anonymous groups that get the 0 index name.

Maybe we could offer a convenience like result.uniqueGroups that contains any matched groups with a unique name. If you need a value that does not have a unique name then you have to go to result.pathname.groups.

@domenic domenic added addition/proposal New features or enhancements and removed enhancement labels Oct 17, 2023
@kettanaito
Copy link

kettanaito commented Jul 18, 2024

Yes, please!

We currently have to iterate over different result groups and merge them into a single groups object with accumulative indexes. A single result.groups would be great to have.

The main issue is what to do for conflicting group names. For example, if both the hostname and pathname have :id groups. Or worse, what if both hostname and pathname have anonymous groups that get the 0 index name.

This is something I wanted to bring up as an issue in general (done in #226). I think URLPattern should support multiple same-named groups within the same component. If those are present, each match will be merged into an array of groups under the same name.

new URLPattern('/foo/:a/bar/:a').exec('/foo/one/bar/two')
// { pathname: { groups: { a: ['one', 'two'] } } }

This also solves your concern above—multiple same-named groups in different URL components will be merged under the same result key. Each respective URL component result groups would contain the entire array (can be a getter to the root-level result.groups of the same name).

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