diff --git a/CHANGES.md b/CHANGES.md index 6ddc59e..1171904 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,6 @@ +## 0.9.3 (unreleased) +* ALL on maps auto-coerces MapEntry to vector, enabling smoother transformation of map keys + ## 0.9.2 * Added VOID selector which navigates nowhere * Better syntax checking for defpath diff --git a/VERSION b/VERSION index 2003b63..65f5c15 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.9.2 +0.9.3-SNAPSHOT diff --git a/src/clj/com/rpl/specter/impl.cljx b/src/clj/com/rpl/specter/impl.cljx index e4d5725..419ea83 100644 --- a/src/clj/com/rpl/specter/impl.cljx +++ b/src/clj/com/rpl/specter/impl.cljx @@ -500,19 +500,30 @@ (assoc structure akey (next-fn (get structure akey)) )) +(defn all-select [structure next-fn] + (into [] (r/mapcat next-fn structure))) + +(defn all-transform [structure next-fn] + (let [empty-structure (empty structure)] + (cond (list? empty-structure) + ;; this is done to maintain order, otherwise lists get reversed + (doall (map next-fn structure)) + + (map? structure) + (->> structure (r/map vec) (r/map next-fn) (into empty-structure)) + + :else + (->> structure (r/map next-fn) (into empty-structure)) + ))) + (deftype AllStructurePath []) (extend-protocol p/StructurePath AllStructurePath (select* [this structure next-fn] - (into [] (r/mapcat next-fn structure))) + (all-select structure next-fn)) (transform* [this structure next-fn] - (let [empty-structure (empty structure)] - (if (list? empty-structure) - ;; this is done to maintain order, otherwise lists get reversed - (doall (map next-fn structure)) - (->> structure (r/map next-fn) (into empty-structure)) - )))) + (all-transform structure next-fn))) (deftype ValCollect []) diff --git a/test/com/rpl/specter/core_test.cljx b/test/com/rpl/specter/core_test.cljx index c0d7d74..1340de9 100644 --- a/test/com/rpl/specter/core_test.cljx +++ b/test/com/rpl/specter/core_test.cljx @@ -615,6 +615,11 @@ ) +(deftest all-map-test + (is (= {3 3} (s/transform [s/ALL s/FIRST] inc {2 3}))) + (is (= {3 21 4 31} (s/transform [s/ALL s/ALL] inc {2 20 3 30}))) + ) + #+clj (deftest large-params-test (let [path (apply s/comp-paths (repeat 25 s/keypath))