Skip to content

Commit

Permalink
Only enable within-string diffing when selected.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alysbrooks committed Jun 10, 2023
1 parent 57dccd1 commit 6d28880
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions src/lambdaisland/deep_diff2/diff_impl.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@
(diff-seq-insertions ins)
(into []))))

(defn diff-string [exp act]
(->> (diff-seq exp act)
(map #(if (char? %) {:= %} %))
(partition-by (comp first keys))
(map (fn [[first-element :as coll]]
(let [head (first (keys first-element))
contents (mapcat vals coll)]
{head (apply str contents) })
)))
)

(defn diff-set [exp act]
(into
(into #{}
Expand Down Expand Up @@ -131,7 +142,7 @@
exp-ks))))

(defn primitive? [x]
(or (number? x) (string? x) (boolean? x) (inst? x) (keyword? x) (symbol? x)))
(or (number? x) (boolean? x) (inst? x) (keyword? x) (symbol? x)))

(defn diff-atom [exp act]
(if (= exp act)
Expand All @@ -151,23 +162,30 @@
(defn array? [x]
(and x (.isArray (class x)))))

(defn diff [exp act]
(cond
(nil? exp)
(diff-atom exp act)
(defn diff
([exp act]
(diff exp act {}))
([exp act {:keys [diff-strings?] :as _opts}]
(cond
(nil? exp)
(diff-atom exp act)

(and (diffable? exp)
(= (data/equality-partition exp) (data/equality-partition act)))
(diff-similar exp act)

(and (diffable? exp)
(= (data/equality-partition exp) (data/equality-partition act)))
(diff-similar exp act)
(array? exp)
(diff-seq exp act)

(array? exp)
(diff-seq exp act)
(record? exp)
(diff-map exp act)

(record? exp)
(diff-map exp act)
(and diff-strings?
(string? exp))
(diff-string exp act)

:else
(diff-atom exp act)))
:else
(diff-atom exp act))))

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

0 comments on commit 6d28880

Please sign in to comment.