Skip to content

Commit 120e9d7

Browse files
committed
add remove-unchanged API
1 parent 61440d4 commit 120e9d7

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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})))))

0 commit comments

Comments
 (0)