howesteve / elegua

Elegua - a small, reactive PWA router for Svelte
https://elegua.netlify.app/
Apache License 2.0
78 stars 4 forks source link

Named parameter doesn't match route when it begins with a number #3

Closed jdryden572 closed 1 year ago

jdryden572 commented 1 year ago

Thanks for sharing this library, I appreciate its simple approach to routing!

It seems like string parameter matching using resolve() will only match a route if the URL path segment begins with letter. For example, if I have the simple routing below:

{#if resolve($path, '/blogs/:id/:slug')}
  <p>Blog ID: {$params['id']}</p>
{:else}
  <p>Oops...</p>
{/if}

This routing will not match in all the scenarios I would expect:

This seems to be because string routing builds a RegExp for string parameters similar to ([a-zA-Z][a-zA-Z0-9\_\-]*). This pattern only matches if the first character is in [a-zA-Z]. Here's a test link for the regex produced by this example for reference: https://regex101.com/r/0ABtfm/1

I can work around this by using a manual regex test instead, like resolve($path, /\/blogs\/(?<id>\w+)\//)

howesteve commented 1 year ago

Thanks for reporting this, and so nicely detailed. I should really have allowed numbers in the named routes. Should be fixed by 72bf30f23d844647079810ea036efa1c572db4d9. I've just bumped it to version 2.0.2.