Skip to content

Commit a4d9ccc

Browse files
ducky427dnolen
authored andcommitted
CLJS-1650: cljs.reader/read-map now returns array-map/hash-map based on the size of the sequence.
1 parent 513ee3a commit a4d9ccc

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/main/cljs/cljs/reader.cljs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,13 @@ nil if the end of stream has been reached")
273273

274274
(defn read-map
275275
[rdr _]
276-
(let [l (read-delimited-list "}" rdr true)]
277-
(when (odd? (count l))
276+
(let [l (read-delimited-list "}" rdr true)
277+
c (count l)]
278+
(when (odd? c)
278279
(reader-error rdr "Map literal must contain an even number of forms"))
279-
(apply hash-map l)))
280+
(if (<= c (* 2 (.-HASHMAP-THRESHOLD PersistentArrayMap)))
281+
(apply array-map l)
282+
(apply hash-map l))))
280283

281284
(defn read-number
282285
[reader initch]

src/test/cljs/cljs/reader_test.cljs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,14 @@
174174
(testing "Testing reading, CLJS-819"
175175
(is (= m " \u00a1")))))
176176

177+
(deftest testing-map-type
178+
(let [a (reader/read-string "{:a 1 :b 2 :c 3}")
179+
b (reader/read-string "{:a 1 :b 2 :c 3 :d 4 :e 5 :f 6 :g 7 :h 8 :i 9}")]
180+
(is (= a {:a 1 :b 2 :c 3}))
181+
(is (instance? PersistentArrayMap a))
182+
(is (= b {:a 1 :b 2 :c 3 :d 4 :e 5 :f 6 :g 7 :h 8 :i 9}))
183+
(is (instance? PersistentHashMap b))))
184+
177185
;; NOTE: issue uncovered by test.check
178186

179187
(deftest test-slash-reading

0 commit comments

Comments
 (0)