Created
November 18, 2012 22:06
-
-
Save mpenet/4107823 to your computer and use it in GitHub Desktop.
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
(defprotocol JSCodec | |
(clj->js [x]) | |
(key->js [x]) | |
(js->clj [x][x options])) | |
(extend-protocol JSCodec | |
default | |
(key->js [k] | |
(if (coll? k) | |
(prn-str k) | |
(clj->js k))) | |
(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) (apply array (map clj->js x)) | |
:else x)) | |
(js->clj | |
([x options] | |
(let [{:keys [keywordize-keys]} options | |
keyfn (if keywordize-keys keyword str) | |
f (fn thisfn [x] | |
(cond | |
(seq? x) (doall (map thisfn x)) | |
(coll? x) (into (empty x) (map thisfn x)) | |
(goog.isArray x) (vec (map thisfn x)) | |
(identical? (type x) js/Object) (into {} (for [k (js-keys x)] | |
[(keyfn k) | |
(thisfn (aget x k))])) | |
:else x))] | |
(f x))) | |
([x] (js->clj x {:keywordize-keys false}))) | |
nil | |
(clj->js [x] nil)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is probably some overhead using a protocol (just a guess) but this would make life easier when dealing with nested structures with custom types (either clojure or js instances depending on the fn).