Fork me on GitHub
#kaocha
<
2018-11-06
>
shaun-mahood18:11:30

I'm guessing this is the best place to ask about https://github.com/lambdaisland/deep-diff Is there a way to only print out the changes between a diff? I've been looking at a custom printer but not sure if that's a dead end.

plexus19:11:54

hi @shaun-mahood, a custom printer should not be necessary, instead traverse the results of diff and remove the stuff that isn't a Insertion/`Deletion`/`Mismatch`

plexus20:11:09

this is only minimally tested but it should give you an idea

plexus20:11:11

(defn difference? [o]
  (some #(instance? % o) #{Mismatch Deletion Insertion}))

(defn contains-difference? [o]
  (cond
    (difference? o)
    true

    (map? o)
    (some contains-difference? (concat (keys o) (vals o)))

    (sequential? o)
    (some contains-difference? o)

    :else
    false))

(defn only-differences [diff]
  (cond
    (difference? diff)
    diff

    (vector? diff)
    (into []
          (comp (map only-differences)
                (filter contains-difference?))
          diff)

    (map? diff)
    (into {}
          (comp (map (fn [[k v]] [k (only-differences v)]))
                (filter (fn [[k v]] (some contains-difference? [k v]))))
          diff)

    (sequential? diff)
    (sequence (comp (map only-differences)
                    (filter contains-difference?))
              diff)

    :else
    diff))

shaun-mahood20:11:18

@plexus Thanks - I thought I might be able to avoid traversing the collections if there was an easy way to change it on the printer side, but that's probably easier. If I get something that works well do you want me to submit it as a PR, or is that out of scope for what you want in the library?

plexus20:11:29

I think it's worth adding, it's been requested before. Feel free to submit a PR.

👍 4