Skip to content

Commit

Permalink
Add index-of
Browse files Browse the repository at this point in the history
  • Loading branch information
darkleaf committed Oct 11, 2024
1 parent ef3a8ff commit b8bf6fa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/medley/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -658,3 +658,13 @@
{: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 @@ -500,3 +500,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 b8bf6fa

Please sign in to comment.