Skip to content

WIP: Add within-string diffing #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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