summaryrefslogtreecommitdiffstats
path: root/addon-sdk/source/lib/diffpatcher/Readme.md
blob: 1520b1c3733d86883d2390f9bba005044be4385e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# diffpatcher

[![Build Status](https://secure.travis-ci.org/Gozala/diffpatcher.png)](http://travis-ci.org/Gozala/diffpatcher)

[![Browser support](https://ci.testling.com/Gozala/diffpatcher.png)](http://ci.testling.com/Gozala/diffpatcher)

Diffpatcher is a small library that lets you treat hashes as if they were
git repositories.

## diff

Diff function that takes two hashes and returns delta hash.

```js
var diff = require("diffpatcher/diff")

diff({ a: { b: 1 }, c: { d: 2 } },      // hash#1
     { a: { e: 3 }, c: { d: 4 } })      // hash#2

// => {                                 // delta
//      a: {
//        b: null,        // -
//        e: 3            // +
//      },
//      c: {
//        d: 4            // ±
//      }
//    }
```

As you can see from the example above `delta` makes no real distinction between
proprety upadate and property addition. Try to think of additions as an update
from `undefined` to whatever it's being updated to.

## patch

Patch fuction takes a `hash` and a `delta` and returns a new `hash` which is
just like orginial but with delta applied to it. Let's apply delta from the
previous example to the first hash from the same example


```js
var patch = require("diffpatcher/patch")

patch({ a: { b: 1 }, c: { d: 2 } },     // hash#1
      {                                 // delta
        a: {
          b: null,        // -
          e: 3            // +
        },
        c: {
          d: 4            // ±
        }
      })

// => { a: { e: 3 }, c: { d: 4 } }      // hash#2
```

That's about it really, just diffing hashes and applying thes diffs on them.


### rebase

And as Linus mentioned everything in git can be expressed with `rebase`, that
also pretty much the case for `diffpatcher`. `rebase` takes `target` hash,
and rebases `parent` onto it with `diff` applied.

## Install

    npm install diffpatcher