mirror of
https://github.com/penpot/penpot.git
synced 2026-09-12 23:19:28 +00:00
🐛 Fix deep-mapm double-applying mfn on leaf entries
The deep-mapm function was applying the mapping function twice on leaf entries (non-map, non-vector values): once when destructuring the entry, and again on the already-transformed result in the else branch. Now mfn is applied exactly once per entry.
This commit is contained in:
parent
a2e6abcb72
commit
057c6ddc0d
@ -602,12 +602,9 @@
|
|||||||
(let [do-map
|
(let [do-map
|
||||||
(fn [entry]
|
(fn [entry]
|
||||||
(let [[k v] (mfn entry)]
|
(let [[k v] (mfn entry)]
|
||||||
(cond
|
(if (or (vector? v) (map? v))
|
||||||
(or (vector? v) (map? v))
|
|
||||||
[k (deep-mapm mfn v)]
|
[k (deep-mapm mfn v)]
|
||||||
|
[k v])))]
|
||||||
:else
|
|
||||||
(mfn [k v]))))]
|
|
||||||
(cond
|
(cond
|
||||||
(map? m)
|
(map? m)
|
||||||
(into {} (map do-map) m)
|
(into {} (map do-map) m)
|
||||||
|
|||||||
@ -538,17 +538,20 @@
|
|||||||
(into [] (d/distinct-xf :id) [{:id 1 :v "a"} {:id 2 :v "x"} {:id 2 :v "b"}]))))
|
(into [] (d/distinct-xf :id) [{:id 1 :v "a"} {:id 2 :v "x"} {:id 2 :v "b"}]))))
|
||||||
|
|
||||||
(t/deftest deep-mapm-test
|
(t/deftest deep-mapm-test
|
||||||
;; Note: mfn is called twice on leaf entries (once initially, once again
|
;; mfn is applied once per entry
|
||||||
;; after checking if the value is a map/vector), so a doubling fn applied
|
(t/is (= {:a 2 :b {:c 4}}
|
||||||
;; to value 1 gives 1*2*2=4.
|
|
||||||
(t/is (= {:a 4 :b {:c 8}}
|
|
||||||
(d/deep-mapm (fn [[k v]] [k (if (number? v) (* v 2) v)])
|
(d/deep-mapm (fn [[k v]] [k (if (number? v) (* v 2) v)])
|
||||||
{:a 1 :b {:c 2}})))
|
{:a 1 :b {:c 2}})))
|
||||||
;; Keyword renaming: keys are also transformed — and applied twice.
|
;; Keyword renaming: keys are transformed once per entry
|
||||||
;; Use an idempotent key transformation (uppercase once = uppercase twice).
|
|
||||||
(let [result (d/deep-mapm (fn [[k v]] [(keyword (str (name k) "!")) v])
|
(let [result (d/deep-mapm (fn [[k v]] [(keyword (str (name k) "!")) v])
|
||||||
{:a 1})]
|
{:a 1})]
|
||||||
(t/is (contains? result (keyword "a!!")))))
|
(t/is (contains? result (keyword "a!"))))
|
||||||
|
;; Vectors inside maps are recursed into
|
||||||
|
(t/is (= {:items [{:x 10}]}
|
||||||
|
(d/deep-mapm (fn [[k v]] [k (if (number? v) (* v 10) v)])
|
||||||
|
{:items [{:x 1}]})))
|
||||||
|
;; Plain scalar at top level map
|
||||||
|
(t/is (= {:a "hello"} (d/deep-mapm identity {:a "hello"}))))
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; Numeric helpers
|
;; Numeric helpers
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user