Created
June 17, 2014 15:25
-
-
Save kurogelee/106ecd82fc8446e81b09 to your computer and use it in GitHub Desktop.
#jsとclj->jsの違いについて ref: http://qiita.com/kurogelee/items/b98d9d924000664a3a48
This file contains hidden or 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 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)))) |
This file contains hidden or 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
(identity #js [[[1 2]]]) | |
(clj->js [[[1 2]]]) |
This file contains hidden or 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 ^: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