File tree Expand file tree Collapse file tree 3 files changed +67
-0
lines changed
src/lambdaisland/deep_diff2
test/lambdaisland/deep_diff2 Expand file tree Collapse file tree 3 files changed +67
-0
lines changed Original file line number Diff line number Diff line change 33## Added
44
55- Enable print tests in babashka
6+ - Add namespace ` lambdaisland.deep-diff2.strip ` ns with ` remove-unchanged ` API
67
78## Fixed
89
Original file line number Diff line number Diff line change 1+ (ns lambdaisland.deep-diff2.strip
2+ " Provide API for manipulate the diff structure data "
3+ (:require [clojure.walk :refer [postwalk]]
4+ #?(:clj [lambdaisland.deep-diff2.diff-impl]
5+ :cljs [lambdaisland.deep-diff2.diff-impl :refer [Mismatch Deletion Insertion]]))
6+ #? (:clj (:import [lambdaisland.deep_diff2.diff_impl Mismatch Deletion Insertion])))
7+
8+ (defn diff-item?
9+ " Checks if x is a Mismatch, Deletion, or Insertion"
10+ [x]
11+ (or (instance? Mismatch x)
12+ (instance? Deletion x)
13+ (instance? Insertion x)))
14+
15+ (defn extend-flatten
16+ " Flatten, which can apply on hashmap"
17+ [x]
18+ (filter (complement coll?)
19+ (rest (tree-seq coll? seq x))))
20+
21+ (defn has-diff-item?
22+ " Checks if there are any diff items in x or sub-tree of x"
23+ [x]
24+ (some
25+ #(or (= :- %) (= :+ %))
26+ (extend-flatten x)))
27+
28+ (defn remove-unchanged
29+ " Postwalk diff, removing values that are unchanged"
30+ [diff]
31+ (postwalk
32+ (fn [x]
33+ (cond
34+ (map-entry? x) (cond
35+ (diff-item? (key x)) (do
36+ ; (println "cond 1, keep" x)
37+ x)
38+ (has-diff-item? (val x)) (do
39+ ; (println "cond 2, keep" x)
40+ x))
41+ :else x))
42+ diff))
43+
Original file line number Diff line number Diff line change 1+ (ns lambdaisland.deep-diff2.strip-test
2+ (:require [clojure.test :refer [deftest testing is are]]
3+ [lambdaisland.deep-diff2.diff-impl :as diff]
4+ [lambdaisland.deep-diff2.strip :as strip]
5+ [lambdaisland.deep-diff2 :as ddiff]))
6+
7+ (deftest strip-test
8+ (testing " removing the same items"
9+ (let [x {:a 1 :b 2 :d {:e 1 } :g [:e [:k 14 :g 15 ]]}
10+ y {:a 1 :c 3 :d {:e 15 } :g [:e [:k 14 :g 15 ]]}]
11+ (is (= (ddiff/diff x y)
12+ {:a 1
13+ (diff/->Deletion :b ) 2
14+ :d {:e (diff/->Mismatch 1 15 )}
15+ :g [:e [:k 14 :g 15 ]]
16+ (diff/->Insertion :c ) 3 }))))
17+ (testing " removing the same items"
18+ (let [x {:a 1 :b 2 :d {:e 1 } :g [:e [:k 14 :g 15 ]]}
19+ y {:a 1 :c 3 :d {:e 15 } :g [:e [:k 14 :g 15 ]]}]
20+ (is (= (strip/remove-unchanged (ddiff/diff x y))
21+ {(diff/->Deletion :b ) 2
22+ :d {:e (diff/->Mismatch 1 15 )}
23+ (diff/->Insertion :c ) 3 })))))
You can’t perform that action at this time.
0 commit comments