From a28282be6219b252dd046733353755d7f6e75d6b Mon Sep 17 00:00:00 2001 From: Asko Nõmm Date: Thu, 26 Jun 2025 12:17:41 +0300 Subject: Fix #7 --- src/ruuter/core.cljc | 20 ++++++++++---------- test/ruuter/core_test.cljc | 13 ++++++++++++- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/ruuter/core.cljc b/src/ruuter/core.cljc index 41201cc..dd325cc 100644 --- a/src/ruuter/core.cljc +++ b/src/ruuter/core.cljc @@ -49,15 +49,6 @@ (cond (= "/" path) {} - (re-find #"\*" path) - (let [index-of-k-start (string/index-of path ":") - k (-> (subs path (+ 1 index-of-k-start)) - drop-last - string/join - keyword) - v (subs uri index-of-k-start)] - {k v}) - :else (let [split-path (->> (string/split path #"/") (remove empty?) @@ -70,8 +61,17 @@ (cond ; required parameter (and (string/starts-with? item ":") - (not (string/ends-with? item "?"))) + (not (string/ends-with? item "?")) + (not (string/ends-with? item "*"))) {(keyword (subs item 1)) (get split-uri idx)} + ; required wildcard parameter + (and (string/starts-with? item ":") + (string/ends-with? item "*")) + {(keyword (-> item + (subs 0 (- (count item) 1)) + (subs 1))) + (->> (drop idx split-uri) + (string/join "/"))} ; optional parameter (and (string/starts-with? item ":") (string/ends-with? item "?") diff --git a/test/ruuter/core_test.cljc b/test/ruuter/core_test.cljc index 7464fe0..3b7d478 100644 --- a/test/ruuter/core_test.cljc +++ b/test/ruuter/core_test.cljc @@ -30,7 +30,18 @@ (testfn "/hello/:who?/:why?" "/hello/world")))) (testing "Wildcard param" (is (= {:everything "this/means/literally/everything"} - (testfn "/hello/:everything*" "/hello/this/means/literally/everything")))))) + (testfn "/hello/:everything*" "/hello/this/means/literally/everything")))) + (testing "Normal params and wildcard param in the end" + (is (= {:id "123" + :path "foo.txt"} + (testfn "/user/:id/file/:path*" "/user/123/file/foo.txt"))) + (is (= {:id "123" + :path "a/b/c/foo.txt"} + (testfn "/user/:id/file/:path*" "/user/123/file/a/b/c/foo.txt"))) + (is (= {:id "123" + :path "a/b/c/foo.txt" + :sub-path "b/c/foo.txt"} + (testfn "/user/:id/file/:path*/:sub-path*" "/user/123/file/a/b/c/foo.txt")))))) (deftest match-route-test (let [testfn #'ruuter/match-route] -- cgit v1.2.3