Skip to content

Commit

Permalink
added deep-merge and deep-merge-with helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
dvcrn committed Dec 9, 2015
1 parent 2db523c commit 99e31aa
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/proton/lib/helpers.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,24 @@
(defn process->html [steps]
(let [steps-html (map #(str "<tr><td class='process-step'>" (get % 0) "</td><td class='process-status'>" (get % 1) "</td></tr>") steps)]
(str "<h2>Welcome to proton<h2><table>" (string/join " " steps-html) "</table>")))

(defn deep-merge
"Deeply merges maps so that nested maps are combined rather than replaced.
For example:
(deep-merge {:foo {:bar :baz}} {:foo {:fuzz :buzz}})
;;=> {:foo {:bar :baz, :fuzz :buzz}}
;; contrast with clojure.core/merge
(merge {:foo {:bar :baz}} {:foo {:fuzz :buzz}})
;;=> {:foo {:fuzz :quzz}} ; note how last value for :foo wins"
[& vs]
(if (every? map? vs)
(apply merge-with deep-merge vs)
(last vs)))

(defn deep-merge-with
"Deeply merges like `deep-merge`, but uses `f` to produce a value from the
conflicting values for a key in multiple maps."
[f & vs]
(if (every? map? vs)
(apply merge-with (partial deep-merge-with f) vs)
(apply f vs)))

0 comments on commit 99e31aa

Please sign in to comment.