Skip to content

Commit

Permalink
CLJS-3290: implement IHash for js Symbol (clojure#225)
Browse files Browse the repository at this point in the history
* implement IHash for js Symbol
* add test cases
  • Loading branch information
swannodette authored and cooljeanius committed Nov 19, 2024
1 parent 5cd3fcc commit 0a8d538
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/cljs/cljs/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1458,6 +1458,11 @@
(-hash [o]
(goog/getUid o)))

(extend-type symbol
IHash
(-hash [o]
(hash (.toString o))))

;;this is primitive because & emits call to array-seq
(defn inc
"Returns a number one greater than num."
Expand Down
9 changes: 9 additions & 0 deletions src/test/cljs/cljs/hashing_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,12 @@
(is (= (hash 0.5) (hash 0.5)))
(is (= (hash -0.32553251) (hash -0.32553251)))
(is (= (hash -0.0000032553251) (hash -0.0000032553251)))))

(deftest test-cljs-3290
(testing "JS Symbol hash"
(let [s (.for js/Symbol "foo")]
(is (number? (hash s)))
(is (== (hash s) (hash s)))
(is (not (== (hash s) (hash (.for js/Symbol "bar")))))
(let [m {s 2}]
(is (== 2 (get m s)))))))

0 comments on commit 0a8d538

Please sign in to comment.