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

Consider replacing pathToRegexp with built-in function #58

Closed
meeroslav opened this issue Nov 12, 2020 · 3 comments
Closed

Consider replacing pathToRegexp with built-in function #58

meeroslav opened this issue Nov 12, 2020 · 3 comments
Assignees

Comments

@meeroslav
Copy link
Collaborator

meeroslav commented Nov 12, 2020

pathToRegexp provides nice functionality for parsing routes, but also includes a lot of functionality we do not use or need.

Simple built-in function could reduce footprint further. Take a look at following:

const wrap = (regexBody: string): string => {
  return `/^${regexBody}/i`
}

const div = '\/';
const end = '$';
const nEnd = '[]|$'; // null or end
const pathEnd = `[\/#\?]`;
const param = `([^\/#\?]+?)`;

const books = wrap(`${div}books(?:${pathEnd}(?=${nEnd}))?(?=${pathEnd}|${nEnd})`); // '/books' + exact false
const login = wrap(`${div}login${pathEnd}?${end}`); // '/login'
const id = wrap(`(?:${div}${param})${pathEnd}?${end}`); // '/:id'
const home = wrap(`${pathEnd}?${end}`); // '/'
const wildcard = wrap(`(?:${pathEnd}(?=${nEnd}))?`); // '/' + exact false

const booksFind = wrap(`${div}books${div}find${pathEnd}?${end}`); // '/books/find'
const booksId = wrap(`${div}books(?:${div}${param})${pathEnd}?${end}`); // '/books/:id'
const booksRoot = wrap(`${div}books${pathEnd}?${end}`); // '/books'
const booksWildcard = wrap(`${div}books(?:${pathEnd}(?=${nEnd}))?(?=${pathEnd}|${nEnd})`); // '/books' + exact false

With these few common patterns we can describe every route matcher Regexp

@meeroslav
Copy link
Collaborator Author

Type checked URL router: https://ja.nsommer.dk/articles/type-checked-url-router.html

@brandonroberts
Copy link
Collaborator

I think this is worth exploring. We also would need to get the path and params info with the internal matcher

@meeroslav
Copy link
Collaborator Author

The path type checking, unfortunately, requires Template Literal Types which landed in TypeScript v4.1. We can park this for later. But internal matcher is still possible.

@meeroslav meeroslav added this to In progress in 0.4.0 release Dec 2, 2020
0.4.0 release automation moved this from In progress to Done Dec 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

No branches or pull requests

2 participants