Created
November 3, 2016 21:03
-
-
Save currentoor/576c25b80e580ef18553087176d94cfd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns arc.router | |
(:require | |
[om.next :as om] | |
[bidi.bidi :as bidi])) | |
(extend-protocol bidi/PatternSegment | |
function | |
(segment-regex-group [this] | |
(condp = this | |
keyword "[A-Za-z]+[A-Za-z0-9\\*\\+\\!\\-\\_\\?\\.]*(?:%2F[A-Za-z]+[A-Za-z0-9\\*\\+\\!\\-\\_\\?\\.]*)?" | |
long "-?\\d{1,19}" | |
bidi/uuid "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}" | |
om/tempid "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}" | |
:otherwise (throw (ex-info (str "Unidentified function qualifier to pattern segment: " this) {})))) | |
(transform-param [this] | |
(condp = this | |
;; keyword is close, but must be applied to a decoded string, to work with namespaced keywords | |
keyword (comp keyword bidi/url-decode) | |
long #(js/Number %) | |
bidi/uuid bidi/uuid | |
om/tempid om/tempid | |
(throw (ex-info (str "Unrecognized function " this) {})))) | |
(matches? [this s] | |
(condp = this | |
keyword (keyword? s) | |
long (not (js/isNaN s)) | |
bidi/uuid (instance? cljs.core.UUID s) | |
om/tempid (om/tempid? s)))) | |
(def routes | |
["/" | |
{"dashboards/" {[[long :id] "/edit"] :dashboards/edit | |
[[bidi/uuid :id] "/edit2"] :dashboards/edit2 | |
[[om/tempid :id] "/edit3"] :dashboards/edit3} | |
"" [[true :dashboards/index]]}]) | |
;; Long | |
(bidi.bidi/path-for routes :dashboards/edit :id 34) | |
=> "/dashboards/34/edit" | |
(bidi.bidi/match-route routes "/dashboards/34/edit") | |
=> {:route-params {:id 34}, :handler :dashboards/edit} | |
;; UUID | |
(bidi/path-for routes :dashboards/edit2 :id (random-uuid)) | |
=> "/dashboards/0079f19d-2a52-476a-a454-b9a28aae1d7c/edit2" | |
(bidi.bidi/match-route routes "/dashboards/e544018a-e3c8-4123-b555-fdf66336eb0e/edit2") | |
=> {:route-params {:id #uuid "e544018a-e3c8-4123-b555-fdf66336eb0e"}, :handler :dashboards/edit2} | |
;; om.next/tempid | |
(bidi/path-for routes :dashboards/edit3 :id (om/tempid)) | |
=> "/dashboards/64033318-13e0-4683-9475-e9743589bd73/edit3" | |
(bidi/match-route routes "/dashboards/e544018a-e3c8-4123-b555-fdf66336eb0e/edit3") | |
=> {:route-params {:id #om/id["e544018a-e3c8-4123-b555-fdf66336eb0e"]}, :handler :dashboards/edit3} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment