Deviopath › TypeScript

URL Router with Params

Hard · Parsing

Write matchRoute(routes, url) that returns the FIRST route pattern in routes that matches url, as { route, params }, or null if none matches.

Rules:

- Ignore everything from ? on in the url (the query string).

- Split patterns and urls on / and ignore empty segments, so "/users/" is the same as "/users" and "" is the same as "/".

- A literal segment must be exactly equal (case-sensitive).

- A segment like :id matches any one segment and stores it in params.id.

- A * segment (only used as the last segment) matches ONE OR MORE remaining segments and stores them joined with / in params["*"].

- Otherwise the number of segments must be equal.

Next: Order Pipeline with Result