added must navigator

This commit is contained in:
Nathan Marz 2016-05-05 14:24:57 -04:00
parent 9d2a5ed46f
commit b4b2200377
4 changed files with 34 additions and 2 deletions

View file

@ -1,3 +1,6 @@
## 0.10.1 (unreleased)
* Added `must` navigator
## 0.10.0 ## 0.10.0
* Make codebase bootstrap cljs compatible * Make codebase bootstrap cljs compatible
* Remove usage of reducers in cljs version in favor of transducers (thanks @StephenRudolph) * Remove usage of reducers in cljs version in favor of transducers (thanks @StephenRudolph)

View file

@ -1 +1 @@
0.10.0 0.10.1-SNAPSHOT

View file

@ -269,13 +269,28 @@
next-val)) next-val))
structure))))) 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] (select* [this structure next-fn]
(next-fn (get structure key))) (next-fn (get structure key)))
(transform* [this structure next-fn] (transform* [this structure next-fn]
(assoc structure key (next-fn (get structure key))) (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] (defpath view [afn]
(select* [this structure next-fn] (select* [this structure next-fn]
(next-fn (afn structure))) (next-fn (afn structure)))

View file

@ -244,6 +244,19 @@
(s/transform (s/view afn) identity i) (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 (defspec parser-test
(for-all+ (for-all+
[i gen/int [i gen/int
@ -835,3 +848,4 @@
(seq? l2) ; Transformed lists are only guaranteed to impelment ISeq (seq? l2) ; Transformed lists are only guaranteed to impelment ISeq
(= q1 q2) (= q1 q2)
(= (type q1) (type q2)))))) (= (type q1) (type q2))))))