Skip to content

Instantly share code, notes, and snippets.

@fivejjs
Forked from jeremyheiler/transform-keys.clj
Created March 25, 2022 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fivejjs/0fce4813fc88666b84fd8cde4b63e77b to your computer and use it in GitHub Desktop.
Save fivejjs/0fce4813fc88666b84fd8cde4b63e77b to your computer and use it in GitHub Desktop.
(defn decode-key
"Converts a train case string into a snake case keyword."
[s]
(keyword (str/replace s "_" "-")))
(defn encode-key
"Converts a snake case keyword into a train case string."
[k]
(str/replace (name k) "-" "_"))
(defn transform-keys
"Recursively transforms all map keys in coll with the transform-key fn."
[transform-key coll]
(letfn [(transform [x] (if (map? x)
(into {} (map (fn [[k v]] [(transform-key k) v]) x))
x))]
(walk/postwalk transform coll)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment