whatwg / urlpattern

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

Repeated groups are not split into an array #146

Open posva opened 3 years ago

posva commented 3 years ago

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.

wanderview commented 3 years ago

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.

posva commented 3 years ago

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