Skip to content

Commit

Permalink
Optimize merge-props
Browse files Browse the repository at this point in the history
  • Loading branch information
kimo-k committed Sep 2, 2024
1 parent 632ce91 commit 30430b7
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/re_com/theme/util.cljs
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
(ns re-com.theme.util
(:require [re-com.util :as u]))

(defn merge-props [& ms]
(let [ms (remove nil? ms)
ms (map #(cond-> % (and (map? %)
(:class %))
(update :class u/->v)) ms)]
(cond
(every? map? ms) (clojure.core/apply merge-with merge-props ms)
(every? vector? ms) (reduce into ms)
:else (last ms))))
#_(defn merge-props [& ms]
(let [ms (remove nil? ms)
ms (map #(cond-> % (and (map? %)
(:class %))
(update :class u/->v)) ms)]
(cond
(every? map? ms) (clojure.core/apply merge-with merge-props ms)
(every? vector? ms) (reduce into ms)
:else (last ms))))

(defn rf [acc {:keys [class attr style] :as m}]
(merge acc (cond-> (if-not (string? m) m {:style [m]})
class
(assoc :class (into (u/->v (:class acc)) (u/->v class)))
attr
(assoc :attr (merge (:attr acc) attr))
style
(assoc :style (merge (:style acc) style)))))

(defn merge-props [& ms] (reduce rf {} ms))

0 comments on commit 30430b7

Please sign in to comment.