summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAsko Nõmm <asko@nmm.ee>2025-06-26 12:17:41 +0300
committerAsko Nõmm <asko@nmm.ee>2025-06-26 12:17:41 +0300
commita28282be6219b252dd046733353755d7f6e75d6b (patch)
tree57e823c9e8239ed8f41e5d06835823ef3e035ad0
parentdd8060ba529a23197fb3773841263498077d7993 (diff)
Fix #7
-rw-r--r--src/ruuter/core.cljc20
-rw-r--r--test/ruuter/core_test.cljc13
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]