Skip to content

Commit 6d28880

Browse files
committed
Only enable within-string diffing when selected.
Create a second arity for backward compatability. For future use, add a general options hash-map. I think diff is a high-level function that might benefit from other options eventually.
1 parent 57dccd1 commit 6d28880

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

src/lambdaisland/deep_diff2/diff_impl.cljc

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@
9797
(diff-seq-insertions ins)
9898
(into []))))
9999

100+
(defn diff-string [exp act]
101+
(->> (diff-seq exp act)
102+
(map #(if (char? %) {:= %} %))
103+
(partition-by (comp first keys))
104+
(map (fn [[first-element :as coll]]
105+
(let [head (first (keys first-element))
106+
contents (mapcat vals coll)]
107+
{head (apply str contents) })
108+
)))
109+
)
110+
100111
(defn diff-set [exp act]
101112
(into
102113
(into #{}
@@ -131,7 +142,7 @@
131142
exp-ks))))
132143

133144
(defn primitive? [x]
134-
(or (number? x) (string? x) (boolean? x) (inst? x) (keyword? x) (symbol? x)))
145+
(or (number? x) (boolean? x) (inst? x) (keyword? x) (symbol? x)))
135146

136147
(defn diff-atom [exp act]
137148
(if (= exp act)
@@ -151,23 +162,30 @@
151162
(defn array? [x]
152163
(and x (.isArray (class x)))))
153164

154-
(defn diff [exp act]
155-
(cond
156-
(nil? exp)
157-
(diff-atom exp act)
165+
(defn diff
166+
([exp act]
167+
(diff exp act {}))
168+
([exp act {:keys [diff-strings?] :as _opts}]
169+
(cond
170+
(nil? exp)
171+
(diff-atom exp act)
172+
173+
(and (diffable? exp)
174+
(= (data/equality-partition exp) (data/equality-partition act)))
175+
(diff-similar exp act)
158176

159-
(and (diffable? exp)
160-
(= (data/equality-partition exp) (data/equality-partition act)))
161-
(diff-similar exp act)
177+
(array? exp)
178+
(diff-seq exp act)
162179

163-
(array? exp)
164-
(diff-seq exp act)
180+
(record? exp)
181+
(diff-map exp act)
165182

166-
(record? exp)
167-
(diff-map exp act)
183+
(and diff-strings?
184+
(string? exp))
185+
(diff-string exp act)
168186

169-
:else
170-
(diff-atom exp act)))
187+
:else
188+
(diff-atom exp act))))
171189

172190
(extend-protocol Diff
173191
#?(:clj java.util.Set :cljs cljs.core/PersistentHashSet)

0 commit comments

Comments
 (0)