Permalink
Browse files

CLJS-322: introduce cljs.core/obj-map

obj-map is guaranteed to produce ObjMap instances (of arbitrary size).
  • Loading branch information...
michalmarczyk authored and David Nolen committed Jun 18, 2012
1 parent 8965df7 commit 16863e3e5cdf2d968fc8d90d2d8da6f00e2398c0
Showing with 20 additions and 0 deletions.
  1. +13 −0 src/cljs/cljs/core.cljs
  2. +7 −0 test/cljs/cljs/core_test.cljs
View
@@ -5437,6 +5437,19 @@ reduces them without incurring seq initialization"
[& keyvals]
(PersistentArrayMap. nil (quot (count keyvals) 2) (apply array keyvals) nil))
+(defn obj-map
+ "keyval => key val
+ Returns a new object map with supplied mappings."
+ [& keyvals]
+ (let [ks (array)
+ obj (js-obj)]
+ (loop [kvs (seq keyvals)]
+ (if kvs
+ (do (.push ks (first kvs))
+ (aset obj (first kvs) (second kvs))
+ (recur (nnext kvs)))
+ (cljs.core.ObjMap/fromObject ks obj)))))
+
(defn sorted-map
"keyval => key val
Returns a new sorted map with supplied mappings."
@@ -1283,6 +1283,13 @@
(assert (= {:foo 'bar} (meta (with-meta (A.) {:foo 'bar}))))
(assert (= 'bar (:foo (assoc (A.) :foo 'bar))))
+ ;; ObjMap
+ (let [ks (map (partial str "foo") (range 500))
+ m (apply obj-map (interleave ks (range 500)))]
+ (assert (instance? cljs.core.ObjMap m))
+ (assert (= 500 (count m)))
+ (assert (= 123 (m "foo123"))))
+
;; dot
(let [s "abc"]
(assert (= 3 (.-length s)))

0 comments on commit 16863e3

Please sign in to comment.