Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/swark/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,18 @@

;; TODO: Support namespaced keywords :-)
(defn ->keyword
"Coerces `input` to a keyword, replacing whitespace with dashes by default."
([input]
(->keyword nil input))
([ignore-match input]
(->keyword ignore-match "-" input))
([ignore-match replacement input]
(if (keyword? input)
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))))))
"Coerces `input` to a keyword, replacing whitespace with dashes by default.
Supports optional keyword arguments:
- `:match`: The regex pattern to replace (default: #\"\\s\").
- `:replacement`: The string to replace matches with (default: \"-\").
Example: `(->keyword \"my input\" :replacement \"_\")` => `:my_input`"
[input & {:keys [replacement match] :or {replacement "-" match #"\s"}}]
(if (keyword? input)
input
(when input
(some-> input name
str/trim str/lower-case
(str/replace match replacement) keyword))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Minimalistic spec
Expand Down
23 changes: 13 additions & 10 deletions test/swark/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,19 @@

(t/deftest ->keyword
(t/are [result args] (= result (apply sut/->keyword args))
:test [:test]
::test [::test]
:hello ["hello"]
:symbol ['symbol]
:h-ell-o1 [" H ell-o1"]
:test/h-ell-o ["test/h ell o"]
:he--o [#"!" "he!!o"]
:hello [#"!" "l" "he!!o"]
:test/hello [#"!" "l" "test/he!!o"]
:hello [#"[0-9\s\-]" "" " H ell-o1"]))
;; Existing cases (no keyword args needed)
:test [:test]
::test [::test]
:hello ["hello"]
:symbol ['symbol]

;; Cases with custom replacements/ignore-match
:h-ell-o1 [" H ell-o1"]
:test/h-ell-o ["test/h ell o"]
:he--o ["he!!o" :replacement "-" :match #"!"]
:hello ["he!!o" :replacement "l" :match #"!"]
:test/hello ["test/he!!o" :replacement "l" :match #"!"]
:hello [" H ell-o1" :replacement "" :match #"[0-9\s\-]"]))

(t/deftest spec
(let [report #::sut{:predicate nat-int? :input -1 :result false}]
Expand Down
Loading