Skip to content

Commit

Permalink
j/lit converts keywords to strings
Browse files Browse the repository at this point in the history
  • Loading branch information
mhuebert committed May 15, 2020
1 parent 3ab19ed commit 2e8576b
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/main/applied_science/js_interop.clj
Expand Up @@ -300,21 +300,29 @@

;; Nested literals (maps/vectors become objects/arrays)

(c/defn litval* [v]
(if (keyword? v)
(cond->> (name v)
(namespace v)
(str (namespace v) "/"))
v))

(c/defn lit*
"Recursively converts literal Clojure maps/vectors into JavaScript object/array expressions
Options map accepts a :keyfn for custom key conversions."
([x]
(lit* nil x))
([{:as opts
:keys [keyfn]
:or {keyfn identity}} x]
:keys [keyfn valfn]
:or {keyfn identity
valfn litval*}} x]
(cond (map? x)
(list* 'applied-science.js-interop/obj
(reduce-kv #(conj %1 (keyfn %2) (lit* opts %3)) [] x))
(vector? x)
(list* 'cljs.core/array (mapv lit* x))
:else x)))
:else (valfn x))))

(defmacro lit
"Recursively converts literal Clojure maps/vectors into JavaScript object/array expressions
Expand Down

0 comments on commit 2e8576b

Please sign in to comment.