addon-sdk/source/lib/diffpatcher/Readme.md

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial