Created
August 20, 2017 15:52
-
-
Save fgui/a2b1193dee2c9e88b00ad64210f1f46f to your computer and use it in GitHub Desktop.
Camels, snakes, pascals and kebabs kata (clojure)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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