Skip to content

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

Closed
@kettanaito

Description

@kettanaito

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    addition/proposalNew features or enhancementsneeds implementer interestMoving the issue forward requires implementers to express interest

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions