From 439d273ff78220a52e8bbc3dbd1f0092ce7cc274 Mon Sep 17 00:00:00 2001 From: Stan Verberkt Date: Mon, 22 Jan 2024 14:21:40 +0100 Subject: [PATCH] Fix ->keyword and test it --- src/swark/core.cljc | 10 ++++++---- test/swark/core_test.clj | 8 ++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/swark/core.cljc b/src/swark/core.cljc index ace6b0c..dec3304 100644 --- a/src/swark/core.cljc +++ b/src/swark/core.cljc @@ -48,12 +48,14 @@ (defn ->keyword "Coerces `input` to a keyword, replacing whitespace with dashes by default." ([input] - (->keyword #"\s" input)) + (->keyword nil input)) ([ignore-match input] (->keyword ignore-match "-" input)) - ([ignore-match replace-with input] - (when input - (some-> input name str/trim str/lower-case (str/replace ignore-match replace-with) keyword)))) + ([ignore-match replacement input] + (let [match (or ignore-match #"\s") + replacement' (or replacement "-")] + (when input + (some-> input name str/trim str/lower-case (str/replace match replacement') keyword))))) (comment (->keyword :test) diff --git a/test/swark/core_test.clj b/test/swark/core_test.clj index 4bea0a2..df3bdb8 100644 --- a/test/swark/core_test.clj +++ b/test/swark/core_test.clj @@ -45,3 +45,11 @@ "string4" " string4 " nil " " nil nil)) + +(t/deftest ->keyword + (t/are [result match replacement input] (= result (sut/->keyword match replacement input)) + :test nil nil :test + :hello nil nil "hello" + :symbol nil nil 'symbol + :h-ell-o1 nil nil " H ell-o1" + :hello #"[0-9\s\-]" "" " H ell-o1"))