urlpattern
urlpattern copied to clipboard
Expose the groups keys in patterns
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.