Created
November 17, 2016 20:57
-
-
Save kennyjwilli/1b6ce5b175ffa5e58357dbc87b7c013f to your computer and use it in GitHub Desktop.
Optimized js->clj implementation by Darrick Wiebe
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
; Optimized js->clj implementation by Darrick Wiebe (http://dev.clojure.org/jira/browse/CLJS-844) | |
; https://github.com/asciinema/asciinema-player/blob/master/src/asciinema/player/patch.cljs | |
(defn js->clj | |
([x] (js->clj x :keywordize-keys false)) | |
([x & opts] | |
(cond | |
(satisfies? IEncodeClojure x) | |
(-js->clj x (apply array-map opts)) | |
(seq opts) | |
(let [{:keys [keywordize-keys]} opts | |
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) | |
(array? x) | |
(persistent! | |
(reduce #(conj! %1 (thisfn %2)) | |
(transient []) x)) | |
(identical? (type x) js/Object) | |
(persistent! | |
(reduce (fn [r k] (assoc! r (keyfn k) (thisfn (aget x k)))) | |
(transient {}) (js-keys x))) | |
:else x))] | |
(f x))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment