Skip to content

Commit 39cca69

Browse files
committed
Merge pull request #23 from airportyh/docs
Prettied up the API formatting a little, and added basic node and web examples
2 parents c6614d9 + a77bd69 commit 39cca69

File tree

6 files changed

+111
-24
lines changed

6 files changed

+111
-24
lines changed

Diff for: README.md

+72-23
Original file line numberDiff line numberDiff line change
@@ -17,54 +17,103 @@ or
1717

1818
## API
1919

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.
2221

2322
Returns a list of change objects (See below).
2423

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.
2725

2826
Returns a list of change objects (See below).
2927

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.
3229

3330
Returns a list of change objects (See below).
3431

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.
3733

3834
Returns a list of change objects (See below).
3935

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.
4237

4338
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
4944

50-
* JsDiff.applyPatch(oldStr, diffStr)
51-
Applies a unified diff patch.
45+
* `JsDiff.applyPatch(oldStr, diffStr)` - applies a unified diff patch.
5246

5347
Return a string containing new version of provided data.
5448

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
5750

5851
### Change Objects
5952
Many of the methods above return change objects. These objects are consist of the following fields:
6053

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
6457

6558
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.
6659

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)**
68117

69118
## License
70119

Diff for: examples/node_example.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require('colors')
2+
var jsdiff = require('../diff');
3+
4+
var one = 'beep boop';
5+
var other = 'beep boob blah';
6+
7+
var diff = jsdiff.diffChars(one, other);
8+
9+
diff.forEach(function(part){
10+
// green for additions, red for deletions
11+
// grey for common parts
12+
var color = part.added ? 'green' :
13+
part.removed ? 'red' : 'grey';
14+
process.stderr.write(part.value[color]);
15+
});
16+
17+
console.log();

Diff for: examples/web_example.html

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<pre id="display"></pre>
2+
<script src="../diff.js"></script>
3+
<script>
4+
var one = 'beep boop';
5+
var other = 'beep boob blah';
6+
7+
var diff = JsDiff.diffChars(one, other);
8+
9+
diff.forEach(function(part){
10+
// green for additions, red for deletions
11+
// grey for common parts
12+
var color = part.added ? 'green' :
13+
part.removed ? 'red' : 'grey';
14+
var span = document.createElement('span');
15+
span.style.color = color;
16+
span.appendChild(document
17+
.createTextNode(part.value));
18+
display.appendChild(span);
19+
});
20+
</script>

Diff for: images/node_example.png

5.52 KB
Loading

Diff for: images/web_example.png

4.27 KB
Loading

Diff for: package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"dependencies": {},
3434
"devDependencies": {
3535
"mocha": "~1.6",
36-
"should": "~1.2"
36+
"should": "~1.2",
37+
"colors": "~0.6.2"
3738
},
3839
"optionalDependencies": {},
3940
"files": [

0 commit comments

Comments
 (0)