Skip to content

Instantly share code, notes, and snippets.

@fgui
Created August 9, 2017 06:22
Show Gist options
  • Save fgui/0e3900a24b4c237ff16605a0b1a6c883 to your computer and use it in GitHub Desktop.
Save fgui/0e3900a24b4c237ff16605a0b1a6c883 to your computer and use it in GitHub Desktop.
(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))))
(defn format [key _ to-format]
(->
key
name
extract-words
((to-format format-functions))
keyword))
(clojure.test/deftest keyword-to-x-case-tests
(clojure.test/are [x y] (= x y)
(format :hello-koko :using :camel-case) :helloKoko
(format :hello-koko :using :snake-case) :hello_koko
(format :hello-koko :using :pascal-case) :HelloKoko
(format :hello-koko :using :kebab-case) :hello-koko
(format :helloKoko :using :kebab-case) :hello-koko))
(keyword-to-x-case-tests)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment