whatwg / urlpattern

URL Pattern Standard
https://urlpattern.spec.whatwg.org/
Other
159 stars 22 forks source link

Multiple same-named groups within the same URL component #226

Closed kettanaito closed 3 months ago

kettanaito commented 4 months ago

What problem are you trying to solve?

I'd like to declare a pattern like this:

/foo/:a/bar/:a

Right now, URLPattern throws an error on such a path pattern:

TypeError: Failed to construct 'URLPattern': invalid pathname pattern '/foo/:a/bar/:a'.

What solutions exist today?

None, if speaking of the solutions provided by URLPattern.

62 can be a related issue to this (supporting multiple same-named groups within the same URL component will solve some of the concerns behind exposing the root-level result.groups).

How would you solve it?

Allow the same URL component to have multiple groups with the same name.

If multiple groups with the same name are present within the same URL component, their matches are merged into an array, where the index represents the match occurrence.

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

Merging the values into an array also helps disambiguate between complex matches, like repeating groups, and multiple same-named groups matches:

// Repeated group match.
new URLPattern('/foo/:a+/bar').exec('/foo/one/two/bar')
// { pathname: { groups: { a: 'one/two' } } }

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

Anything else?

In comparison, path-to-regexp also doesn't support this behavior, but neither does it throw. It accepts the path syntax, just doesn't merge the values (the latest value is considered the whole value of the group):

var {match} = require("path-to-regexp")

const m = match('/foo/:a/bar/:a')
m('/foo/one/bar/two')
// {"path":"/foo/one/bar/two","index":0,"params":{"a":"two"}}

This also means that URLPattern has this as a difference between itself and path-to-regexp but it's not mentioned in this section:

Currently we plan to have these known differences with path-to-regexp:

No support for custom prefixes and suffixes.
kettanaito commented 4 months ago

It's also interesting that nowhere in the spec I can see any mention about group names having to be unique within the same URL component. Feel free to link that, I couldn't find it. If there is no such mention, then I'd treat this as a bug, not a feature proposal.

kettanaito commented 4 months ago

I also have an interest in fixing this given a bit of guidance.

kettanaito commented 3 months ago

@wanderview, I apologize for a direct ping, just wanted to check if this is considered a bug (which I believe it is). Thanks.

sisidovski commented 3 months ago

Sorry for the slow response!

Regarding the duplicated name, there is a mention here. So this is not a bug.

In comparison, path-to-regexp also doesn't support this behavior, but neither does it throw. It accepts the path syntax, just doesn't merge the values (the latest value is considered the whole value of the group):

URLPattern used to behave like this before, but it was changed to throw an error in https://github.com/whatwg/urlpattern/issues/96, and I think that direction makes sense.

kettanaito commented 3 months ago

Thanks for the response, @sisidovski! Got it. Will have to make it a breaking change on my end then. Closing as not a bug.