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

Non-capturing groups support for match-routes #15

Open
ripley-on-rails opened this issue Apr 11, 2014 · 1 comment
Open

Non-capturing groups support for match-routes #15

ripley-on-rails opened this issue Apr 11, 2014 · 1 comment

Comments

@ripley-on-rails
Copy link

Hello,

First let me start by expressing my thanks for the time and effort spent creating bidi. We use enjoy bidi as part of our routing.

There is something I came to notice about route matching which I think that could be improved. Often one will have to create routes with an optional param like a page postfix so that paths such as "/foo" and "/foo/page-2" are recognised.

As far as I understand this can be achieved like so:

Without page:

user> (bidi/match-route ["/foo" :foo]
                        "/foo")
{:handler :foo}

With page:

user> (bidi/match-route [["/foo" "/page-" :page] :foo]
                        "/foo/page-1")
{:handler :foo, :params {:page "1"}}

Both combined:

user> (map (partial bidi/match-route ["/foo" {"" :foo 
                                              ["/page-" :page] :foo}])
           ["/foo" "/foo/page-2"])
({:handler :foo} {:handler :foo, :params {:page "2"}})

However, since there is support for regex I tried to write a more compact version by using non-capturing groups to exclude irrelevant parts such "/page-" like so:

user> (map (partial bidi/match-route 
                    [["/foo" [#"(?:/page-(\d+))?" :page]] :foo])
           ["/foo" "/foo/page-2"])
({:handler :foo, :params {:page ""}} 
 {:handler :foo, :params {:page "/page-2"}})

Apparently the capturing and non-capturing groups are ignored. What I was hoping for was something like the previous result:

({:handler :foo} {:handler :foo, :params {:page "2"}})

I'm not sure if it's worth the effort but such a change might be helpful in reducing certain redundancies when defining routes with optional path-params. What do you think?

Regards,

Roman

@cch1
Copy link

cch1 commented Jan 16, 2017

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants