diff --git a/modules/reitit-core/src/reitit/core.cljc b/modules/reitit-core/src/reitit/core.cljc index 0dc5d6a7..43d7656f 100644 --- a/modules/reitit-core/src/reitit/core.cljc +++ b/modules/reitit-core/src/reitit/core.cljc @@ -339,7 +339,7 @@ (defn mixed-router "Creates two routers: [[lookup-router]] or [[single-static-path-router]] for - static routes and [[prefix-tree-router]] for wildcard routes. All + static routes and [[segment-router]] for wildcard routes. All routes should be non-conflicting. Takes resolved routes and optional expanded options. See [[router]] for options." ([routes] @@ -348,7 +348,7 @@ (let [{wild true, lookup false} (group-by impl/wild-route? routes) compiled (compile-routes routes opts) ->static-router (if (= 1 (count lookup)) single-static-path-router lookup-router) - wildcard-router (prefix-tree-router wild opts) + wildcard-router (segment-router wild opts) static-router (->static-router lookup opts) names (find-names routes opts)] ^{:type ::router} @@ -399,7 +399,7 @@ (and (= 1 (count routes)) (not wilds?)) single-static-path-router conflicting linear-router (not wilds?) lookup-router - all-wilds? prefix-tree-router + all-wilds? segment-router :else mixed-router)] (when-let [conflicts (:conflicts opts)] diff --git a/modules/reitit-core/src/reitit/impl.cljc b/modules/reitit-core/src/reitit/impl.cljc index 7bbae117..4a962b48 100644 --- a/modules/reitit-core/src/reitit/impl.cljc +++ b/modules/reitit-core/src/reitit/impl.cljc @@ -34,8 +34,9 @@ (defn wild-or-catch-all-param? [x] (boolean (or (wild-param x) (catch-all-param x)))) -(defn segments [^String path] - (into [] (.split path "/" 666))) +(defn segments [path] + #?(:clj (.split ^String path "/" 666) + :cljs (.split path #"/" 666))) (defn contains-wilds? [path] (boolean (some wild-or-catch-all-param? (segments path)))) diff --git a/modules/reitit-core/src/reitit/segment.cljc b/modules/reitit-core/src/reitit/segment.cljc index dc84b06c..2e9316d2 100644 --- a/modules/reitit-core/src/reitit/segment.cljc +++ b/modules/reitit-core/src/reitit/segment.cljc @@ -48,8 +48,8 @@ (insert segment p data)) nil paths)) -(defn lookup [segment ^String path] - (-lookup segment (.split path "/") {})) +(defn lookup [segment path] + (-lookup segment (impl/segments path) {})) (comment (-> [["/:abba" 1] diff --git a/test/cljc/reitit/core_test.cljc b/test/cljc/reitit/core_test.cljc index 9f684faa..ef518ffa 100644 --- a/test/cljc/reitit/core_test.cljc +++ b/test/cljc/reitit/core_test.cljc @@ -45,20 +45,27 @@ #"^missing path-params for route /api/ipa/:size -> \#\{:size\}$" (r/match-by-name! router ::beer)))))) + ;; TODO (testing "complex" (let [router (r/router - [["/:abba" ::abba] - ["/abba/1" ::abba1] - ["/:abba/:dabba/doo" ::doo] - ["/abba/:dabba/boo" ::boo]] {:router r}) + [["/:abba" ::abba] + ["/abba/1" ::abba2] + ["/:jabba/2" ::jabba2] + ["/:abba/:dabba/doo" ::doo] + ["/abba/:dabba/boo" ::boo] + #_["/:jabba/:dabba/:doo/*foo" ::wild]] + {:router r}) matches #(-> router (r/match-by-path %) :data :name)] (is (= ::abba (matches "/abba"))) - (is (= ::abba1 (matches "/abba/1"))) + (is (= ::abba2 (matches "/abba/1"))) + (is (= ::jabba2 (matches "/abba/2"))) (is (= ::doo (matches "/abba/1/doo"))) - (is (= ::boo (matches "/abba/1/boo")))))) + (is (= ::boo (matches "/abba/1/boo"))) + #_(is (= ::wild (matches "/olipa/kerran/avaruus/vaan/ei/toista/kertaa"))) + ))) r/linear-router :linear-router - r/prefix-tree-router :prefix-tree-router + #_#_r/prefix-tree-router :prefix-tree-router r/segment-router :segment-router r/mixed-router :mixed-router)) diff --git a/test/cljc/reitit/impl_test.cljc b/test/cljc/reitit/impl_test.cljc new file mode 100644 index 00000000..dced731a --- /dev/null +++ b/test/cljc/reitit/impl_test.cljc @@ -0,0 +1,9 @@ +(ns reitit.impl-test + (:require [clojure.test :refer [deftest testing is are]] + [reitit.impl :as impl])) + +(deftest segments-test + (is (= ["" "api" "ipa" "beer" "craft" "bisse"] + (into [] (impl/segments "/api/ipa/beer/craft/bisse")))) + (is (= ["" "a" "" "b" "" "c" ""] + (into [] (impl/segments "/a//b//c/"))))) diff --git a/test/cljs/reitit/doo_runner.cljs b/test/cljs/reitit/doo_runner.cljs index aaa2fc1b..57b7dc2b 100644 --- a/test/cljs/reitit/doo_runner.cljs +++ b/test/cljs/reitit/doo_runner.cljs @@ -2,6 +2,7 @@ (:require [doo.runner :refer-macros [doo-tests]] reitit.coercion-test reitit.core-test + reitit.impl-test reitit.middleware-test reitit.ring-test #_reitit.spec-test)) @@ -10,6 +11,7 @@ (doo-tests 'reitit.coercion-test 'reitit.core-test + 'reitit.impl-test 'reitit.middleware-test 'reitit.ring-test #_'reitit.spec-test)