Guest User

Untitled

a guest
Jul 20th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. (defn js->clj
  2. "Recursively transforms JavaScript arrays into ClojureScript
  3. vectors, and JavaScript objects into ClojureScript maps. With
  4. option ':keywordize-keys true' will convert object fields from
  5. strings to keywords."
  6. ([x] (js->clj x :keywordize-keys false))
  7. ([x & opts]
  8. (let [{:keys [keywordize-keys]} opts
  9. keyfn (if keywordize-keys keyword str)
  10. f (fn thisfn [x]
  11. (cond
  12. (satisfies? IEncodeClojure x)
  13. (-js->clj x (apply array-map opts))
  14.  
  15. (seq? x)
  16. (doall (map thisfn x))
  17.  
  18. (map-entry? x)
  19. (MapEntry. (thisfn (key x)) (thisfn (val x)) nil)
  20.  
  21. (coll? x)
  22. (into (empty x) (map thisfn) x)
  23.  
  24. (array? x)
  25. (persistent!
  26. (reduce #(conj! %1 (thisfn %2))
  27. (transient []) x))
  28.  
  29. (identical? (type x) js/Object)
  30. (persistent!
  31. (reduce (fn [r k] (assoc! r (keyfn k) (thisfn (gobject/get x k))))
  32. (transient {}) (js-keys x)))
  33. :else x))]
  34. (f x))))
Add Comment
Please, Sign In to add comment