Skip to content

Commit

Permalink
Change index-of to return nil if value not found
Browse files Browse the repository at this point in the history
This makes it consistent with clojure.string/index-of.
  • Loading branch information
weavejester committed Oct 18, 2024
1 parent 923d478 commit b2dd5d0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/medley/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -661,9 +661,9 @@

(defn index-of
"Returns the index of the first occurrence of the item in the sequential
collection coll, or -1 if not found."
collection coll, or nil if not found."
{:added "1.9.0"}
[^java.util.List coll item]
(if (nil? coll)
-1
(.indexOf coll item)))
(when (some? coll)
(let [index (.indexOf coll item)]
(when-not (neg? index) index))))
10 changes: 5 additions & 5 deletions test/medley/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -504,12 +504,12 @@
(is (not (m/regexp? nil))))

(deftest test-index-of
(is (= -1 (m/index-of nil :a)))
(is (= -1 (m/index-of [] :a)))
(is (= -1 (m/index-of '() :a)))
(is (nil? (m/index-of nil :a)))
(is (nil? (m/index-of [] :a)))
(is (nil? (m/index-of '() :a)))

(is (= -1 (m/index-of [:a] :b)))
(is (= -1 (m/index-of '(:a) :b)))
(is (nil? (m/index-of [:a] :b)))
(is (nil? (m/index-of '(:a) :b)))

(is (= 1 (m/index-of [:a :b :c :d] :b)))
(is (= 2 (m/index-of '(:a :b :c :d) :c)))
Expand Down

0 comments on commit b2dd5d0

Please sign in to comment.