From 6d28880058ddb3c14ef54b79f778eec915a75e78 Mon Sep 17 00:00:00 2001 From: Alys Brooks Date: Fri, 9 Jun 2023 22:47:11 -0500 Subject: [PATCH] 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. --- src/lambdaisland/deep_diff2/diff_impl.cljc | 46 +++++++++++++++------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/src/lambdaisland/deep_diff2/diff_impl.cljc b/src/lambdaisland/deep_diff2/diff_impl.cljc index e864f87..6b61af9 100644 --- a/src/lambdaisland/deep_diff2/diff_impl.cljc +++ b/src/lambdaisland/deep_diff2/diff_impl.cljc @@ -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 #{} @@ -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) @@ -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)