Skip to content

Commit

Permalink
Merge pull request #95 from darkleaf/index-of
Browse files Browse the repository at this point in the history
Add index-of function
  • Loading branch information
weavejester authored Oct 18, 2024
2 parents df8e233 + 55f6e22 commit 923d478
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/medley/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -658,3 +658,12 @@
{:added "1.4.0"}
[x]
(instance? #?(:clj java.util.regex.Pattern :cljs js/RegExp) x))

(defn index-of
"Returns the index of the first occurrence of the item in the sequential
collection coll, or -1 if not found."
{:added "1.9.0"}
[^java.util.List coll item]
(if (nil? coll)
-1
(.indexOf coll item)))
14 changes: 14 additions & 0 deletions test/medley/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -502,3 +502,17 @@
(is (m/regexp? #"x"))
(is (not (m/regexp? "x")))
(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 (= -1 (m/index-of [:a] :b)))
(is (= -1 (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)))

(is (= 1 (m/index-of (range 0 10) 1)))
(is (= 1 (m/index-of (map str [:a :b]) ":b"))))

0 comments on commit 923d478

Please sign in to comment.