Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Added

- Enable print tests in babashka
- Add namespace `lambdaisland.deep-diff2.strip` ns with `remove-unchanged` API

## Fixed

Expand Down
43 changes: 43 additions & 0 deletions src/lambdaisland/deep_diff2/strip.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
(ns lambdaisland.deep-diff2.strip
"Provide API for manipulate the diff structure data "
(:require [clojure.walk :refer [postwalk]]
#?(:clj [lambdaisland.deep-diff2.diff-impl]
:cljs [lambdaisland.deep-diff2.diff-impl :refer [Mismatch Deletion Insertion]]))
#?(:clj (:import [lambdaisland.deep_diff2.diff_impl Mismatch Deletion Insertion])))

(defn diff-item?
"Checks if x is a Mismatch, Deletion, or Insertion"
[x]
(or (instance? Mismatch x)
(instance? Deletion x)
(instance? Insertion x)))

(defn extend-flatten
"Flatten, which can apply on hashmap"
[x]
(filter (complement coll?)
(rest (tree-seq coll? seq x))))

(defn has-diff-item?
"Checks if there are any diff items in x or sub-tree of x"
[x]
(some
#(or (= :- %) (= :+ %))
(extend-flatten x)))

(defn remove-unchanged
"Postwalk diff, removing values that are unchanged"
[diff]
(postwalk
(fn [x]
(cond
(map-entry? x) (cond
(diff-item? (key x)) (do
;(println "cond 1, keep" x)
x)
(has-diff-item? (val x)) (do
;(println "cond 2, keep" x)
x))
:else x))
diff))

23 changes: 23 additions & 0 deletions test/lambdaisland/deep_diff2/strip_test.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(ns lambdaisland.deep-diff2.strip-test
(:require [clojure.test :refer [deftest testing is are]]
[lambdaisland.deep-diff2.diff-impl :as diff]
[lambdaisland.deep-diff2.strip :as strip]
[lambdaisland.deep-diff2 :as ddiff]))

(deftest strip-test
(testing "removing the same items"
(let [x {:a 1 :b 2 :d {:e 1} :g [:e [:k 14 :g 15]]}
y {:a 1 :c 3 :d {:e 15} :g [:e [:k 14 :g 15]]}]
(is (= (ddiff/diff x y)
{:a 1
(diff/->Deletion :b) 2
:d {:e (diff/->Mismatch 1 15)}
:g [:e [:k 14 :g 15]]
(diff/->Insertion :c) 3}))))
(testing "removing the same items"
(let [x {:a 1 :b 2 :d {:e 1} :g [:e [:k 14 :g 15]]}
y {:a 1 :c 3 :d {:e 15} :g [:e [:k 14 :g 15]]}]
(is (= (strip/remove-unchanged (ddiff/diff x y))
{(diff/->Deletion :b) 2
:d {:e (diff/->Mismatch 1 15)}
(diff/->Insertion :c) 3})))))