js-obj

function/macrosince v0.0-927Edit
(js-obj)
(js-obj & keyvals)

Details:

Returns a new JavaScript object using the supplied mappings.

keyvals must be an even number of forms.


Examples:

(js-obj "foo" 1 "bar" 2)
;;=> #js {:foo 1, :bar 2}

See Also:


Source docstring:
Create JavaSript object from an even number arguments representing
interleaved keys and values.
Function code @ clojurescript:src/main/cljs/cljs/core.cljs
(defn js-obj
  ([]
     (cljs.core/js-obj))
  ([& keyvals]
     (apply gobject/create keyvals)))

Macro code @ clojurescript:src/main/clojure/cljs/core.cljc
(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]] `(goog.object/set ~obj ~k ~v)) sym-pairs)
        ~@(map (core/fn [[k v]] `(goog.object/set ~obj ~v ~(core/get kvs k))) expr->local)
        ~obj))))