Skip to content

Instantly share code, notes, and snippets.

@kurogelee
Created June 17, 2014 15:25
Show Gist options
  • Save kurogelee/106ecd82fc8446e81b09 to your computer and use it in GitHub Desktop.
Save kurogelee/106ecd82fc8446e81b09 to your computer and use it in GitHub Desktop.
#jsとclj->jsの違いについて ref: http://qiita.com/kurogelee/items/b98d9d924000664a3a48
(defn clj->js
"Recursively transforms ClojureScript values to JavaScript.
sets/vectors/lists become Arrays, Keywords and Symbol become Strings,
Maps become Objects. Arbitrary keys are encoded to by key->js."
[x]
(when-not (nil? x)
(if (satisfies? IEncodeJS x)
(-clj->js x)
(cond
(keyword? x) (name x)
(symbol? x) (str x)
(map? x) (let [m (js-obj)]
(doseq [[k v] x]
(aset m (key->js k) (clj->js v)))
m)
(coll? x) (let [arr (array)]
(doseq [x (map clj->js x)]
(.push arr x))
arr)
:else x))))
(identity #js [[[1 2]]])
(clj->js [[[1 2]]])
(defn ^:private read-js
[form]
(cond
(vector? form)
(let [arr (array)]
(doseq [x form]
(.push arr x))
arr)
(map? form)
(let [obj (js-obj)]
(doseq [[k v] form]
(aset obj (name k) v))
obj)
:else
(reader-error nil
(str "JS literal expects a vector or map containing "
"only string or unqualified keyword keys"))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment