Skip to content

Instantly share code, notes, and snippets.

@kennyjwilli
Created November 17, 2016 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kennyjwilli/1b6ce5b175ffa5e58357dbc87b7c013f to your computer and use it in GitHub Desktop.
Save kennyjwilli/1b6ce5b175ffa5e58357dbc87b7c013f to your computer and use it in GitHub Desktop.
Optimized js->clj implementation by Darrick Wiebe
; 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