diff --git a/CHANGES.md b/CHANGES.md index c1fbcd3..d366767 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,6 @@ +## 0.10.1 (unreleased) +* Added `must` navigator + ## 0.10.0 * Make codebase bootstrap cljs compatible * Remove usage of reducers in cljs version in favor of transducers (thanks @StephenRudolph) diff --git a/VERSION b/VERSION index 78bc1ab..d4e167b 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.10.0 +0.10.1-SNAPSHOT diff --git a/src/clj/com/rpl/specter.cljx b/src/clj/com/rpl/specter.cljx index 645156f..cf5340a 100644 --- a/src/clj/com/rpl/specter.cljx +++ b/src/clj/com/rpl/specter.cljx @@ -269,13 +269,28 @@ next-val)) structure))))) -(defpath keypath [key] +(defpath + ^{:doc "Navigates to the specified key, navigating to nil if it does not exist."} + keypath + [key] (select* [this structure next-fn] (next-fn (get structure key))) (transform* [this structure next-fn] (assoc structure key (next-fn (get structure key))) )) +(defpath + ^{:doc "Navigates to the key only if it exists in the map."} + must + [k] + (select* [this structure next-fn] + (next-fn (get structure k))) + (transform* [this structure next-fn] + (if (contains? structure k) + (assoc structure k (next-fn (get structure k))) + structure + ))) + (defpath view [afn] (select* [this structure next-fn] (next-fn (afn structure))) diff --git a/test/com/rpl/specter/core_test.cljx b/test/com/rpl/specter/core_test.cljx index 9501db1..74dfe2a 100644 --- a/test/com/rpl/specter/core_test.cljx +++ b/test/com/rpl/specter/core_test.cljx @@ -244,6 +244,19 @@ (s/transform (s/view afn) identity i) ))) +(defspec must-test + (for-all+ + [k1 gen/int + k2 (gen/such-that #(not= k1 %) gen/int) + m (gen-map-with-keys gen/int gen/int k1) + op (gen/elements [inc dec]) + ] + (let [m (dissoc m k2)] + (and (= (s/transform (s/must k1) op m) + (s/transform (s/keypath k1) op m)) + (= (s/transform (s/must k2) op m) m) + )))) + (defspec parser-test (for-all+ [i gen/int @@ -835,3 +848,4 @@ (seq? l2) ; Transformed lists are only guaranteed to impelment ISeq (= q1 q2) (= (type q1) (type q2)))))) +