function/macro | since v0.0-927 | Edit |
(js-obj)
(js-obj & keyvals)
Returns a new JavaScript object using the supplied mappings.
keyvals
must be an even number of forms.
(js-obj "foo" 1 "bar" 2)
;;=> #js {:foo 1, :bar 2}
Create JavaSript object from an even number arguments representing interleaved keys and values.
(defn js-obj
([]
(cljs.core/js-obj))
([& keyvals]
(apply gobject/create keyvals)))
(core/defmacro js-obj [& rest]
(core/let [sym-or-str? (core/fn [x] (core/or (core/symbol? x) (core/string? x)))
filter-on-keys (core/fn [f coll]
(core/->> coll
(filter (core/fn [[k _]] (f k)))
(into {})))
kvs (into {} (map vec (partition 2 rest)))
sym-pairs (filter-on-keys core/symbol? kvs)
expr->local (zipmap
(filter (complement sym-or-str?) (keys kvs))
(repeatedly gensym))
obj (gensym "obj")]
(if (empty? rest)
(js-obj* '())
`(let [~@(apply concat (clojure.set/map-invert expr->local))
~obj ~(js-obj* (filter-on-keys core/string? kvs))]
~@(map (core/fn [[k v]] `(unchecked-set ~obj ~k ~v)) sym-pairs)
~@(map (core/fn [[k v]] `(unchecked-set ~obj ~v ~(core/get kvs k))) expr->local)
~obj))))