https://github.com/ul/differ
A library to diff and patch clojure(script) datastructures
https://github.com/ul/differ
Last synced: over 1 year ago
JSON representation
A library to diff and patch clojure(script) datastructures
- Host: GitHub
- URL: https://github.com/ul/differ
- Owner: ul
- Created: 2014-11-23T08:34:29.000Z (over 11 years ago)
- Default Branch: development
- Last Pushed: 2014-11-23T18:34:30.000Z (over 11 years ago)
- Last Synced: 2025-03-08T17:08:55.822Z (over 1 year ago)
- Language: Clojure
- Size: 145 KB
- Stars: 10
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
Awesome Lists containing this project
README
# Differ [](http://travis-ci.org/#!/Skinney/differ/builds)
A library for diffing, and patching, Clojure(script) datastructures.
## Motivation
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.
## Setup
Add the following the to your `project.clj`:
[](http://clojars.org/differ)
## Usage
First of all, you need to require the proper namespace:
```clojure
(ns some.ns
(:require [differ.core :as differ]))
```
You can create a diff using the `differ.core/diff` function:
```clojure
(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:
```clojure
(differ/patch {:specie :dog
:sex :female}
person-diff)
;; Will return {:name "Robin Heggelund Hansen"
;; :age 26
;; :specie :dog}
```
## Contributing
Feedback to both this library and this guide is welcome. Plese read `CONTRIBUTING.md` for more information.
### Running the tests
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:
```bash
λ lein all-tests
```
## License
Copyright © 2014 Robin Heggelund Hansen.
Distributed under the [MIT License](http://opensource.org/licenses/MIT).