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

Repeated groups are not split into an array #146

Open
posva opened this issue Nov 10, 2021 · 2 comments
Open

Repeated groups are not split into an array #146

posva opened this issue Nov 10, 2021 · 2 comments
Labels
addition/proposal New features or enhancements

Comments

@posva
Copy link

posva commented Nov 10, 2021

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 posva changed the title Repeated params are not split into an array Repeated groups are not split into an array Nov 10, 2021
@wanderview
Copy link
Member

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
Copy link
Author

posva commented Nov 10, 2021

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 #147 in order to apply custom transformations to groups

@domenic domenic added addition/proposal New features or enhancements and removed enhancement labels Oct 17, 2023
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