urlpattern icon indicating copy to clipboard operation
urlpattern copied to clipboard

Repeated groups are not split into an array

Open posva opened this issue 4 years ago • 2 comments

Hello, I'm testing out the URLPattern in Chrome canary against the test suite I use for Vue Router and I found that repeated groups (/:id+ and /:id*) are not put into an array when extracted:

const path = '/:id+'
const pathToTest = '/a/b/c'

const pattern = new URLPattern(path, 'http://e.e')

const match = pattern.exec(pathToTest, 'http://e.e')
console.assert(JSON.stringify(match?.pathname.groups) === '["a","b","c"]', `Got ${JSON.stringify(match?.pathname.groups)} instead of array`)

This will output:

Assertion failed: Got {"id":"a/b/c"} instead of array

While it's easy to make this work by calling .split('/'), I think id should be an array because its modifier is a repeatable one.

posva avatar Nov 10 '21 09:11 posva

I think this one is working as intended. Its behavior inherited from path-to-regexp.

Also, its unclear to me how to implement this in the general sense. Consider that a repeated modifier might be applied to something without a prefix; e.g. foo{:g}+bar. Also consider that :g is a non-greedy match meaning that "each group" would match as few characters as possible. So if we split this into an array, then matching against foobazbar would result in a g array where each letter is a separate array index like ['b', 'a', 'z']. This doesn't seem expected or desirable to me.

I think I would prefer to leave it to external code to do the splitting in a way that made sense to them for now. If it becomes super common then maybe we could adopt and option to auto-split on a given divider or something in the future.

wanderview avatar Nov 10 '21 13:11 wanderview

I see, to me, repeatable groups can only be between / e.g: /:id+ but never /a-:id+-b, so I never faced anything like foo:g+bar. Semantically speaking I do expect a list for a repeatable group though but I think that's maybe only from the router perspective. So it would make sense to keep this behavior in the context of a pattern

To me this reinforces the need of https://github.com/WICG/urlpattern/issues/147 in order to apply custom transformations to groups

posva avatar Nov 10 '21 14:11 posva