extend ImplicitNav for strings, numbers, characters, booleans, symbols, and regexes

This commit is contained in:
nathanmarz 2017-11-08 13:47:40 -05:00
parent 349e03342f
commit 5b60eb17e3
3 changed files with 34 additions and 2 deletions

View file

@ -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

View file

@ -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."}

View file

@ -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)