diff --git a/src/medley/core.cljc b/src/medley/core.cljc index 1e9ab08..355f42e 100644 --- a/src/medley/core.cljc +++ b/src/medley/core.cljc @@ -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))) diff --git a/test/medley/core_test.cljc b/test/medley/core_test.cljc index 0d4981b..f43f6e7 100644 --- a/test/medley/core_test.cljc +++ b/test/medley/core_test.cljc @@ -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"))))