|
1 # diffpatcher |
|
2 |
|
3 [](http://travis-ci.org/Gozala/diffpatcher) |
|
4 |
|
5 [](http://ci.testling.com/Gozala/diffpatcher) |
|
6 |
|
7 Diffpatcher is a small library that lets you treat hashes as if they were |
|
8 git repositories. |
|
9 |
|
10 ## diff |
|
11 |
|
12 Diff function that takes two hashes and returns delta hash. |
|
13 |
|
14 ```js |
|
15 var diff = require("diffpatcher/diff") |
|
16 |
|
17 diff({ a: { b: 1 }, c: { d: 2 } }, // hash#1 |
|
18 { a: { e: 3 }, c: { d: 4 } }) // hash#2 |
|
19 |
|
20 // => { // delta |
|
21 // a: { |
|
22 // b: null, // - |
|
23 // e: 3 // + |
|
24 // }, |
|
25 // c: { |
|
26 // d: 4 // ± |
|
27 // } |
|
28 // } |
|
29 ``` |
|
30 |
|
31 As you can see from the example above `delta` makes no real distinction between |
|
32 proprety upadate and property addition. Try to think of additions as an update |
|
33 from `undefined` to whatever it's being updated to. |
|
34 |
|
35 ## patch |
|
36 |
|
37 Patch fuction takes a `hash` and a `delta` and returns a new `hash` which is |
|
38 just like orginial but with delta applied to it. Let's apply delta from the |
|
39 previous example to the first hash from the same example |
|
40 |
|
41 |
|
42 ```js |
|
43 var patch = require("diffpatcher/patch") |
|
44 |
|
45 patch({ a: { b: 1 }, c: { d: 2 } }, // hash#1 |
|
46 { // delta |
|
47 a: { |
|
48 b: null, // - |
|
49 e: 3 // + |
|
50 }, |
|
51 c: { |
|
52 d: 4 // ± |
|
53 } |
|
54 }) |
|
55 |
|
56 // => { a: { e: 3 }, c: { d: 4 } } // hash#2 |
|
57 ``` |
|
58 |
|
59 That's about it really, just diffing hashes and applying thes diffs on them. |
|
60 |
|
61 |
|
62 ### rebase |
|
63 |
|
64 And as Linus mentioned everything in git can be expressed with `rebase`, that |
|
65 also pretty much the case for `diffpatcher`. `rebase` takes `target` hash, |
|
66 and rebases `parent` onto it with `diff` applied. |
|
67 |
|
68 ## Install |
|
69 |
|
70 npm install diffpatcher |