diff --git a/CHANGES.md b/CHANGES.md index 3a816e6..f5a8b8c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,8 @@ ## 1.0.5-SNAPSHOT * Add `regex-nav` navigator for regexes, which navigates to every match in a string and supports replacement with a new substring (thanks @mwfogleman) +* Regexes implicitly convert to `regex-nav` in paths +* Strings, numbers, booleans, characters, and symbols implicitly convert to `keypath` in paths ## 1.0.4 diff --git a/src/clj/com/rpl/specter.cljc b/src/clj/com/rpl/specter.cljc index a2670f4..e482d35 100644 --- a/src/clj/com/rpl/specter.cljc +++ b/src/clj/com/rpl/specter.cljc @@ -1169,6 +1169,26 @@ ImplicitNav (implicit-nav [this] (n/keypath* this))) +(extend-type #?(:clj clojure.lang.Symbol :cljs cljs.core/Symbol) + ImplicitNav + (implicit-nav [this] (n/keypath* this))) + +(extend-type #?(:clj String :cljs string) + ImplicitNav + (implicit-nav [this] (n/keypath* this))) + +(extend-type #?(:clj Number :cljs number) + ImplicitNav + (implicit-nav [this] (n/keypath* this))) + +(extend-type #?(:clj Character :cljs char) + ImplicitNav + (implicit-nav [this] (n/keypath* this))) + +(extend-type #?(:clj Boolean :cljs boolean) + ImplicitNav + (implicit-nav [this] (n/keypath* this))) + (extend-type #?(:clj clojure.lang.AFn :cljs function) ImplicitNav (implicit-nav [this] (pred this))) @@ -1177,6 +1197,10 @@ ImplicitNav (implicit-nav [this] (pred this))) +(extend-type #?(:clj java.util.regex.Pattern :cljs js/RegExp) + ImplicitNav + (implicit-nav [this] (regex-nav this))) + (defnav ^{:doc "Navigates to the provided val if the structure is nil. Otherwise it stays navigated at the structure."} diff --git a/test/com/rpl/specter/core_test.cljc b/test/com/rpl/specter/core_test.cljc index 6ab8dd6..3ed621a 100644 --- a/test/com/rpl/specter/core_test.cljc +++ b/test/com/rpl/specter/core_test.cljc @@ -1435,10 +1435,12 @@ ) (deftest regex-navigation-test - (is (= (select (s/regex-nav #"t") "test") ["t" "t"])) + ;; also test regexes as implicit navs + (is (= (select #"t" "test") ["t" "t"])) (is (= (select [:a (s/regex-nav #"t")] {:a "test"}) ["t" "t"])) (is (= (transform (s/regex-nav #"t") clojure.string/capitalize "test") "TesT")) - (is (= (transform [:a (s/regex-nav #"t")] clojure.string/capitalize {:a "test"}) {:a "TesT"})) + ;; also test regexes as implicit navs + (is (= (transform [:a #"t"] clojure.string/capitalize {:a "test"}) {:a "TesT"})) (is (= (transform (s/regex-nav #"\s+\w") clojure.string/triml "Hello World!") "HelloWorld!")) (is (= (setval (s/regex-nav #"t") "z" "test") "zesz")) (is (= (setval [:a (s/regex-nav #"t")] "z" {:a "test"}) {:a "zesz"})) @@ -1644,6 +1646,10 @@ (is (= [[1 :a] [2 :b] [3 :c]] (select (s/indexed-vals 1) [:a :b :c]))) )) +(deftest other-implicit-navs-test + (is (= 1 (select-any ["a" true \c 10 'd] {"a" {true {\c {10 {'d 1}}}}}))) + ) + #?(:clj (do (defprotocolpath FooPP)