urlpattern icon indicating copy to clipboard operation
urlpattern copied to clipboard

Expose the groups keys in patterns

Open posva opened this issue 3 years ago • 2 comments

When creating a pattern like new URLPattern('/:id/static/:tags*'), there is no way of knowing what groups the pattern has (id and tags) nor their nature (optional, repeatable).

I propose a way of getting these groups:

const pattern = new URLPattern('/:id/static/:tags*')

pattern.pathname.value // '/:id/static/:tags*'
pattern.pathname.groups // { id: { optional: false, repeatable: false }, tags: { optional: true, repeatable: true }}
// or maybe with flags
const GROUP_REPEATABLE = 1
const GROUP_OPTIONAL = 2
const GROUP_REPEATABLE_OPTIONAL = GROUP_REPEATABLE & GROUP_OPTIONAL
pattern.pathname.groups // { id: 0, tags: 3 }

It's also worth noting this can be typed in TS too with string literals.

posva avatar Nov 10 '21 10:11 posva