Implement cljify.
This commit is contained in:
parent
1d53d547b8
commit
ea027e8cea
2 changed files with 54 additions and 0 deletions
47
src/browser/datomish/cljify.cljs
Normal file
47
src/browser/datomish/cljify.cljs
Normal file
|
@ -0,0 +1,47 @@
|
|||
(ns datomish.cljify)
|
||||
|
||||
(defn cljify
|
||||
"Does what `(js->clj o :keywordize-keys true) is supposed to do, but works
|
||||
in environments with more than one context (e.g., web browsers).
|
||||
|
||||
See <http://dev.clojure.org/jira/browse/CLJS-439?focusedCommentId=43909>.
|
||||
|
||||
Note that Date instances are passed through."
|
||||
[o]
|
||||
(cond
|
||||
(nil? o)
|
||||
nil
|
||||
|
||||
;; Primitives.
|
||||
(or
|
||||
(true? o)
|
||||
(false? o)
|
||||
(number? o)
|
||||
(string? o)
|
||||
;; Dates are passed through.
|
||||
(not (nil? (aget (aget o "__proto__") "getUTCMilliseconds"))))
|
||||
o
|
||||
|
||||
;; Array.
|
||||
(.isArray js/Array o)
|
||||
(let [n (.-length o)]
|
||||
(loop [i 0
|
||||
acc (transient [])]
|
||||
(if (< i n)
|
||||
(recur (inc i) (conj! acc (cljify (aget o i))))
|
||||
(persistent! acc))))
|
||||
|
||||
;; Object.
|
||||
(not (nil? (aget (aget o "__proto__") "hasOwnProperty")))
|
||||
(let [a (.keys js/Object o)
|
||||
n (.-length a)]
|
||||
(loop [i 0
|
||||
acc (transient {})]
|
||||
(if (< i n)
|
||||
(let [key (aget a i)]
|
||||
(recur (inc i) (assoc! acc
|
||||
(keyword key)
|
||||
(cljify (aget o key)))))
|
||||
(persistent! acc))))
|
||||
|
||||
:else o))
|
7
src/node/datomish/cljify.cljs
Normal file
7
src/node/datomish/cljify.cljs
Normal file
|
@ -0,0 +1,7 @@
|
|||
(ns datomish.cljify)
|
||||
|
||||
(defn cljify
|
||||
"In node, equivalent to `(js->clj o :keywordize-keys true).
|
||||
See <http://dev.clojure.org/jira/browse/CLJS-439?focusedCommentId=43909>."
|
||||
[o]
|
||||
(js->clj o :keywordize-keys true))
|
Loading…
Reference in a new issue