diff --git a/src/clj/com/rpl/specter.cljx b/src/clj/com/rpl/specter.cljx index d1b403e..c0802bf 100644 --- a/src/clj/com/rpl/specter.cljx +++ b/src/clj/com/rpl/specter.cljx @@ -270,6 +270,13 @@ (next-fn (afn structure)) )) +(defpath parser [parse-fn unparse-fn] + (select* [this structure next-fn] + (next-fn (parse-fn structure))) + (transform* [this structure next-fn] + (unparse-fn (next-fn (parse-fn structure))) + )) + (defn selected? "Filters the current value based on whether a selector finds anything. e.g. (selected? :vals ALL even?) keeps the current element only if an diff --git a/test/com/rpl/specter/core_test.cljx b/test/com/rpl/specter/core_test.cljx index 9e11022..6693ead 100644 --- a/test/com/rpl/specter/core_test.cljx +++ b/test/com/rpl/specter/core_test.cljx @@ -242,6 +242,18 @@ (s/transform (s/view afn) identity i) ))) +(defspec parser-test + (for-all+ + [i gen/int + afn (gen/elements [inc dec #(* % 2)]) + bfn (gen/elements [inc dec #(* % 2)]) + cfn (gen/elements [inc dec #(* % 2)])] + (and (= (first (s/select (s/parser afn bfn) i)) + (afn i)) + (= (s/transform (s/parser afn bfn) cfn i) + (-> i afn cfn bfn)) + ))) + (deftest selected?-test (is (= [[1 3 5] [2 :a] [7 11 4 2 :a] [10 1 :a] []] (s/setval [s/ALL (s/selected? s/ALL even?) s/END]