|
17 | 17 |
|
18 | 18 | ## API
|
19 | 19 |
|
20 |
| -* JsDiff.diffChars(oldStr, newStr) |
21 |
| - Diffs two blocks of text, comparing character by character. |
| 20 | +* `JsDiff.diffChars(oldStr, newStr)` - diffs two blocks of text, comparing character by character. |
22 | 21 |
|
23 | 22 | Returns a list of change objects (See below).
|
24 | 23 |
|
25 |
| -* JsDiff.diffWords(oldStr, newStr) |
26 |
| - Diffs two blocks of text, comparing word by word. |
| 24 | +* `JsDiff.diffWords(oldStr, newStr)` - diffs two blocks of text, comparing word by word. |
27 | 25 |
|
28 | 26 | Returns a list of change objects (See below).
|
29 | 27 |
|
30 |
| -* JsDiff.diffLines(oldStr, newStr) |
31 |
| - Diffs two blocks of text, comparing line by line. |
| 28 | +* `JsDiff.diffLines(oldStr, newStr)` - diffs two blocks of text, comparing line by line. |
32 | 29 |
|
33 | 30 | Returns a list of change objects (See below).
|
34 | 31 |
|
35 |
| -* JsDiff.diffCss(oldStr, newStr) |
36 |
| - Diffs two blocks of text, comparing CSS tokens. |
| 32 | +* `JsDiff.diffCss(oldStr, newStr)` - diffs two blocks of text, comparing CSS tokens. |
37 | 33 |
|
38 | 34 | Returns a list of change objects (See below).
|
39 | 35 |
|
40 |
| -* JsDiff.createPatch(fileName, oldStr, newStr, oldHeader, newHeader) |
41 |
| - Creates a unified diff patch. |
| 36 | +* `JsDiff.createPatch(fileName, oldStr, newStr, oldHeader, newHeader)` - creates a unified diff patch. |
42 | 37 |
|
43 | 38 | Parameters:
|
44 |
| - * fileName : String to be output in the filename sections of the patch |
45 |
| - * oldStr : Original string value |
46 |
| - * newStr : New string value |
47 |
| - * oldHeader : Additional information to include in the old file header |
48 |
| - * newHeader : Additional information to include in thew new file header |
| 39 | + * `fileName` : String to be output in the filename sections of the patch |
| 40 | + * `oldStr` : Original string value |
| 41 | + * `newStr` : New string value |
| 42 | + * `oldHeader` : Additional information to include in the old file header |
| 43 | + * `newHeader` : Additional information to include in thew new file header |
49 | 44 |
|
50 |
| -* JsDiff.applyPatch(oldStr, diffStr) |
51 |
| - Applies a unified diff patch. |
| 45 | +* `JsDiff.applyPatch(oldStr, diffStr)` - applies a unified diff patch. |
52 | 46 |
|
53 | 47 | Return a string containing new version of provided data.
|
54 | 48 |
|
55 |
| -* convertChangesToXML(changes) |
56 |
| - Converts a list of changes to a serialized XML format |
| 49 | +* `convertChangesToXML(changes)` - converts a list of changes to a serialized XML format |
57 | 50 |
|
58 | 51 | ### Change Objects
|
59 | 52 | Many of the methods above return change objects. These objects are consist of the following fields:
|
60 | 53 |
|
61 |
| -* value: Text content |
62 |
| -* added: True if the value was inserted into the new string |
63 |
| -* removed: True of the value was removed from the old string |
| 54 | +* `value`: Text content |
| 55 | +* `added`: True if the value was inserted into the new string |
| 56 | +* `removed`: True of the value was removed from the old string |
64 | 57 |
|
65 | 58 | Note that some cases may omit a particular flag field. Comparison on the flag fields should always be done in a truthy or falsy manner.
|
66 | 59 |
|
67 |
| -## [Example](http://kpdecker.github.com/jsdiff) |
| 60 | +## Examples |
| 61 | + |
| 62 | +Basic example in Node |
| 63 | + |
| 64 | +```js |
| 65 | +require('colors') |
| 66 | +var jsdiff = require('diff'); |
| 67 | + |
| 68 | +var one = 'beep boop'; |
| 69 | +var other = 'beep boob blah'; |
| 70 | + |
| 71 | +var diff = jsdiff.diffChars(one, other); |
| 72 | + |
| 73 | +diff.forEach(function(part){ |
| 74 | + // green for additions, red for deletions |
| 75 | + // grey for common parts |
| 76 | + var color = part.added ? 'green' : |
| 77 | + part.removed ? 'red' : 'grey'; |
| 78 | + process.stderr.write(part.value[color]); |
| 79 | +}); |
| 80 | + |
| 81 | +console.log() |
| 82 | +``` |
| 83 | +Running the above program should yield |
| 84 | + |
| 85 | +<img src="images/node_example.png" alt="Node Example"> |
| 86 | + |
| 87 | +Basic example in a web page |
| 88 | + |
| 89 | +```html |
| 90 | +<pre id="display"></pre> |
| 91 | +<script src="diff.js"></script> |
| 92 | +<script> |
| 93 | +var one = 'beep boop'; |
| 94 | +var other = 'beep boob blah'; |
| 95 | +
|
| 96 | +var diff = JsDiff.diffChars(one, other); |
| 97 | +
|
| 98 | +diff.forEach(function(part){ |
| 99 | + // green for additions, red for deletions |
| 100 | + // grey for common parts |
| 101 | + var color = part.added ? 'green' : |
| 102 | + part.removed ? 'red' : 'grey'; |
| 103 | + var span = document.createElement('span'); |
| 104 | + span.style.color = color; |
| 105 | + span.appendChild(document |
| 106 | + .createTextNode(part.value)); |
| 107 | + display.appendChild(span); |
| 108 | +}); |
| 109 | +</script> |
| 110 | +``` |
| 111 | + |
| 112 | +Open the above .html file in a browser and you should see |
| 113 | + |
| 114 | +<img src="images/web_example.png" alt="Node Example"> |
| 115 | + |
| 116 | +**[Full online demo](http://kpdecker.github.com/jsdiff)** |
68 | 117 |
|
69 | 118 | ## License
|
70 | 119 |
|
|
0 commit comments