A library for diffing, and patching, Clojure(script) datastructures.
I wanted to implement an auto-save feature for my Clojurescript web-app. To be efficient, only the actual changes should be sent to the backend. clojure.data/diff
is not the easiest function to work with for several reasons, and there didn't seem to be any good alternatives, so differ was born.
Add the following the to your project.clj
:
First of all, you need to require the proper namespace:
(ns some.ns
(:require [differ.core :as differ]))
You can create a diff using the differ.core/diff
function:
(def person-map {:name "Robin"
:age 25
:sex :male})
(def person-diff (differ/diff test-map {:name "Robin Heggelund Hansen"
:age 26})
;; person-diff will now be [{:name "Robin Heggelund Hansen", :age 26}
;; {:sex 0}]
differ.core/diff
will return a datastructure of the same type that is given, and will work with nested datastructures. If you only want alterations, or removals, instead of both, please check the differ.diff
and differ.patch
namespaces.
NOTE: Currently, only maps are supported.
To apply the diff, you can use the differ.core/patch
function. This function works on any similar datastructure:
(differ/patch {:specie :dog
:sex :female}
person-diff)
;; Will return {:name "Robin Heggelund Hansen"
;; :age 26
;; :specie :dog}
Feedback to both this library and this guide is welcome. Plese read CONTRIBUTING.md
for more information.
Differ is assumed to work with Clojure 1.6 and up, as well as Clojurescript 2371 and up.
There is a leiningen alias that makes it easy to run the tests against supported Clojure versions:
λ lein all-tests
Copyright © 2014 Robin Heggelund Hansen.
Distributed under the MIT License.