Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defn js->clj
- "Recursively transforms JavaScript arrays into ClojureScript
- vectors, and JavaScript objects into ClojureScript maps. With
- option ':keywordize-keys true' will convert object fields from
- strings to keywords."
- ([x] (js->clj x :keywordize-keys false))
- ([x & opts]
- (let [{:keys [keywordize-keys]} opts
- keyfn (if keywordize-keys keyword str)
- f (fn thisfn [x]
- (cond
- (satisfies? IEncodeClojure x)
- (-js->clj x (apply array-map opts))
- (seq? x)
- (doall (map thisfn x))
- (map-entry? x)
- (MapEntry. (thisfn (key x)) (thisfn (val x)) nil)
- (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 (gobject/get x k))))
- (transient {}) (js-keys x)))
- :else x))]
- (f x))))
Add Comment
Please, Sign In to add comment