Skip to content

Instantly share code, notes, and snippets.

@runexec
Last active August 29, 2015 14:12
Show Gist options
  • Save runexec/d2e90953ee392e101f92 to your computer and use it in GitHub Desktop.
Save runexec/d2e90953ee392e101f92 to your computer and use it in GitHub Desktop.
Clojure style string conversion

Clojure Style String Conversion

(def example-str "anExampleSTRINGthatCanBeConverted")

(defn conversion-handler [x]
  (let [r clojure.string/replace]
    (-> x        
        (r #"[a-z]{1}[A-Z]{1}"
           (fn [x]
             (let [[x y] x]
               (str x "-" y))))
        (r #"[A-Z]{2}[a-z]{1}"
           (fn [x]
             (let [[x y z] x]             
               (str x  y "-" z))))
        (r #"^[A-Z]+-"
           (fn [x]
             (let [body (butlast x)]
               (str (reduce str (butlast body))
                    "-"
                    (last body)))))
        (r #"_" "-")
        clojure.string/lower-case)))
example.core> (conversion-handler example-str)
"an-example-string-that-can-be-converted"
example.core> (conversion-handler "MMARecord")
"mma-record"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment