diff --git a/src/browser/datomish/cljify.cljs b/src/browser/datomish/cljify.cljs new file mode 100644 index 00000000..7c02f5bb --- /dev/null +++ b/src/browser/datomish/cljify.cljs @@ -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 . + + 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)) diff --git a/src/node/datomish/cljify.cljs b/src/node/datomish/cljify.cljs new file mode 100644 index 00000000..3ef64a3f --- /dev/null +++ b/src/node/datomish/cljify.cljs @@ -0,0 +1,7 @@ +(ns datomish.cljify) + +(defn cljify + "In node, equivalent to `(js->clj o :keywordize-keys true). + See ." + [o] + (js->clj o :keywordize-keys true))