Skip to content

Commit 184083e

Browse files
wilkerlucioswannodette
authored andcommitted
CLJS-3283 Support -contains-key? protocol check in contains?
1 parent aa2f0e6 commit 184083e

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/main/cljs/cljs/core.cljs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2360,8 +2360,14 @@ reduces them without incurring seq initialization"
23602360
range of indexes. 'contains?' operates constant or logarithmic time;
23612361
it will not perform a linear search for a value. See also 'some'."
23622362
[coll v]
2363-
(if (identical? (get coll v lookup-sentinel) lookup-sentinel)
2363+
(cond
2364+
(implements? IAssociative coll)
2365+
(-contains-key? coll v)
2366+
2367+
(identical? (get coll v lookup-sentinel) lookup-sentinel)
23642368
false
2369+
2370+
:else
23652371
true))
23662372

23672373
(defn find

src/test/cljs/cljs/core_test.cljs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,14 @@
8484
(is (not (contains? (to-array [5 6 7]) 3)))
8585
(is (not (contains? nil 42)))
8686
(is (contains? "f" 0))
87-
(is (not (contains? "f" 55)))))
87+
(is (not (contains? "f" 55))))
88+
89+
(testing "Testing contains? with IAssociative protocol"
90+
(let [ds (reify
91+
IAssociative
92+
(-contains-key? [_ k] (= k :valid)))]
93+
(is (contains? ds :valid))
94+
(is (not (contains? ds :invalid))))))
8895

8996
(deftest test-run!
9097
(testing "Testing run!"

0 commit comments

Comments
 (0)