Skip to content

Instantly share code, notes, and snippets.

@fgui
Created August 20, 2017 15:52
Show Gist options
  • Save fgui/a2b1193dee2c9e88b00ad64210f1f46f to your computer and use it in GitHub Desktop.
Save fgui/a2b1193dee2c9e88b00ad64210f1f46f to your computer and use it in GitHub Desktop.
Camels, snakes, pascals and kebabs kata (clojure)
;; https://gist.github.com/trikitrok/a97d330bacb1f56fe5ee027c12ff273a
(require '[clojure.test :as test])
(def format-functions
{:snake-case (fn [words]
(clojure.string/join "_" words))
:kebab-case (fn [words]
(clojure.string/join "-" words))
:camel-case (fn [words]
(apply str (first words)
(map clojure.string/capitalize (rest words))))
:pascal-case (fn [words]
(apply str
(map clojure.string/capitalize words)))})
(defn extract-words [a-str]
(map clojure.string/lower-case
(filter #(not (#{"-" "_"} %))
(re-seq #"[A-Z]?[a-z]+|-|_" a-str))))
(defprotocol FormatCase
(format-case [data verb to-format]))
(extend clojure.lang.Keyword
FormatCase
{
:format-case (fn [data _ to-format]
(->
data
name
extract-words
((to-format format-functions))
keyword))})
(format-case :hello-koko :using :camel-case)
(clojure.test/deftest keyword-to-x-case-tests
(clojure.test/are [x y] (= x y)
(format-case :hello-koko :using :camel-case) :helloKoko
(format-case :hello-koko :using :snake-case) :hello_koko
(format-case :hello-koko :using :pascal-case) :HelloKoko
(format-case :hello-koko :using :kebab-case) :hello-koko
(format-case :helloKoko :using :kebab-case) :hello-koko))
(keyword-to-x-case-tests)
(extend clojure.lang.Keyword
FormatCase
{
:format-case (fn [data _ to-format]
(->
data
name
extract-words
((to-format format-functions))
keyword))})
(clojure.test/deftest keyword-to-x-case-box-tests
(clojure.test/are [x y] (= x y)
(format-case [:hello-koko :hello_koko] :using :camel-case) [:helloKoko :helloKoko]
(format-case #{:hello-koko :hello_koko} :using :camel-case) #{:helloKoko}
(format-case {:hello-koko 1 :HelloLolo :hello_koko} :using :camel-case) {:helloKoko 1 :helloLolo :helloKoko}))
(keyword-to-x-case-box-tests)
(extend java.lang.Object
FormatCase
{
:format-case (fn [this x to-format]
this)})
(extend clojure.lang.APersistentSet
FormatCase
{
:format-case (fn [this x to-format]
(set (map #(format-case % x to-format) this)))})
(extend clojure.lang.APersistentVector
FormatCase
{
:format-case (fn [this x to-format]
(mapv #(format-case % x to-format) this))})
(extend clojure.lang.APersistentMap
FormatCase
{
:format-case (fn [this x to-format]
(into {} (map (fn [[k v]]
[(format-case k x to-format)
(format-case v x to-format)]) this)))})
(clojure.test/deftest keyword-to-x-case-recur-tests
(clojure.test/are [x y] (= x y)
(format-case [:hello-koko :hello_koko #{:hello-koko :hello_koko}
{:hello-koko 1 :HelloLolo :hello_koko}]
:using :camel-case ) [:helloKoko :helloKoko #{:helloKoko} {:helloKoko 1 :helloLolo :helloKoko}]))
(keyword-to-x-case-recur-tests)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment