Created
August 11, 2011 23:21
-
-
Save lynaghk/1141054 to your computer and use it in GitHub Desktop.
Clojure sequentials & maps into JavaScript arrays and objects
This file contains 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
(defn jsArr | |
"Recursively converts a sequential object into a JavaScript array" | |
[seq] | |
(.array (vec (map #(if (sequential? %) (jsArr %) %) | |
seq)))) | |
(defn jsObj | |
"Convert a clojure map into a JavaScript object" | |
[obj] | |
(.strobj (into {} (map (fn [[k v]] | |
(let [k (if (keyword? k) (name k) k) | |
v (if (keyword? v) (name v) v)] | |
(if (map? v) | |
[k (jsObj v)] | |
[k v]))) | |
obj)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment