diff --git a/.jshintrc b/.jshintrc
index d57cb294..3db30150 100644
--- a/.jshintrc
+++ b/.jshintrc
@@ -1,7 +1,6 @@
/*dotfiles node, mocha*/
{
"node": true,
- "es5": true,
"esnext": true,
"browser": false,
"bitwise": true,
@@ -38,4 +37,4 @@
"beforeEach",
"afterEach"
]
-}
\ No newline at end of file
+}
diff --git a/README.md b/README.md
index 95bd8da2..1bc32d5a 100644
--- a/README.md
+++ b/README.md
@@ -17,54 +17,103 @@ or
## API
-* JsDiff.diffChars(oldStr, newStr)
- Diffs two blocks of text, comparing character by character.
+* `JsDiff.diffChars(oldStr, newStr)` - diffs two blocks of text, comparing character by character.
Returns a list of change objects (See below).
-* JsDiff.diffWords(oldStr, newStr)
- Diffs two blocks of text, comparing word by word.
+* `JsDiff.diffWords(oldStr, newStr)` - diffs two blocks of text, comparing word by word.
Returns a list of change objects (See below).
-* JsDiff.diffLines(oldStr, newStr)
- Diffs two blocks of text, comparing line by line.
+* `JsDiff.diffLines(oldStr, newStr)` - diffs two blocks of text, comparing line by line.
Returns a list of change objects (See below).
-* JsDiff.diffCss(oldStr, newStr)
- Diffs two blocks of text, comparing CSS tokens.
+* `JsDiff.diffCss(oldStr, newStr)` - diffs two blocks of text, comparing CSS tokens.
Returns a list of change objects (See below).
-* JsDiff.createPatch(fileName, oldStr, newStr, oldHeader, newHeader)
- Creates a unified diff patch.
+* `JsDiff.createPatch(fileName, oldStr, newStr, oldHeader, newHeader)` - creates a unified diff patch.
Parameters:
- * fileName : String to be output in the filename sections of the patch
- * oldStr : Original string value
- * newStr : New string value
- * oldHeader : Additional information to include in the old file header
- * newHeader : Additional information to include in thew new file header
+ * `fileName` : String to be output in the filename sections of the patch
+ * `oldStr` : Original string value
+ * `newStr` : New string value
+ * `oldHeader` : Additional information to include in the old file header
+ * `newHeader` : Additional information to include in thew new file header
-* JsDiff.applyPatch(oldStr, diffStr)
- Applies a unified diff patch.
+* `JsDiff.applyPatch(oldStr, diffStr)` - applies a unified diff patch.
Return a string containing new version of provided data.
-* convertChangesToXML(changes)
- Converts a list of changes to a serialized XML format
+* `convertChangesToXML(changes)` - converts a list of changes to a serialized XML format
### Change Objects
Many of the methods above return change objects. These objects are consist of the following fields:
-* value: Text content
-* added: True if the value was inserted into the new string
-* removed: True of the value was removed from the old string
+* `value`: Text content
+* `added`: True if the value was inserted into the new string
+* `removed`: True of the value was removed from the old string
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.
-## [Example](http://kpdecker.github.com/jsdiff)
+## Examples
+
+Basic example in Node
+
+```js
+require('colors')
+var jsdiff = require('diff');
+
+var one = 'beep boop';
+var other = 'beep boob blah';
+
+var diff = jsdiff.diffChars(one, other);
+
+diff.forEach(function(part){
+ // green for additions, red for deletions
+ // grey for common parts
+ var color = part.added ? 'green' :
+ part.removed ? 'red' : 'grey';
+ process.stderr.write(part.value[color]);
+});
+
+console.log()
+```
+Running the above program should yield
+
+
+
+Basic example in a web page
+
+```html
+
+
+
+```
+
+Open the above .html file in a browser and you should see
+
+
+
+**[Full online demo](http://kpdecker.github.com/jsdiff)**
## License
@@ -99,3 +148,7 @@ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVI
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+
+[](https://bitdeli.com/free "Bitdeli Badge")
+
diff --git a/diff.js b/diff.js
index a34c22a0..c084609a 100644
--- a/diff.js
+++ b/diff.js
@@ -165,7 +165,22 @@ var JsDiff = (function() {
var LineDiff = new Diff();
LineDiff.tokenize = function(value) {
- return value.split(/^/m);
+ var retLines = [],
+ lines = value.split(/^/m);
+
+ for(var i = 0; i < lines.length; i++) {
+ var line = lines[i],
+ lastLine = lines[i - 1];
+
+ // Merge lines that may contain windows new lines
+ if (line == '\n' && lastLine && lastLine[lastLine.length - 1] === '\r') {
+ retLines[retLines.length - 1] += '\n';
+ } else if (line) {
+ retLines.push(line);
+ }
+ }
+
+ return retLines;
};
return {
diff --git a/examples/node_example.js b/examples/node_example.js
new file mode 100644
index 00000000..0c62ca1b
--- /dev/null
+++ b/examples/node_example.js
@@ -0,0 +1,17 @@
+require('colors')
+var jsdiff = require('../diff');
+
+var one = 'beep boop';
+var other = 'beep boob blah';
+
+var diff = jsdiff.diffChars(one, other);
+
+diff.forEach(function(part){
+ // green for additions, red for deletions
+ // grey for common parts
+ var color = part.added ? 'green' :
+ part.removed ? 'red' : 'grey';
+ process.stderr.write(part.value[color]);
+});
+
+console.log();
\ No newline at end of file
diff --git a/examples/web_example.html b/examples/web_example.html
new file mode 100644
index 00000000..fc58df7e
--- /dev/null
+++ b/examples/web_example.html
@@ -0,0 +1,20 @@
+
+
+
\ No newline at end of file
diff --git a/images/node_example.png b/images/node_example.png
new file mode 100644
index 00000000..eb87034a
Binary files /dev/null and b/images/node_example.png differ
diff --git a/images/web_example.png b/images/web_example.png
new file mode 100644
index 00000000..aa7e0def
Binary files /dev/null and b/images/web_example.png differ
diff --git a/package.json b/package.json
index 4c5d6fc0..7f5318fa 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "diff",
- "version": "1.0.7",
+ "version": "1.0.8",
"description": "A javascript text diff implementation.",
"keywords": [
"diff",
@@ -33,7 +33,8 @@
"dependencies": {},
"devDependencies": {
"mocha": "~1.6",
- "should": "~1.2"
+ "should": "~1.2",
+ "colors": "~0.6.2"
},
"optionalDependencies": {},
"files": [
diff --git a/release-notes.md b/release-notes.md
index 6989e2bb..4667f8b8 100644
--- a/release-notes.md
+++ b/release-notes.md
@@ -2,7 +2,13 @@
## Development
-[Commits](https://github.com/kpdecker/jsdiff/compare/v1.0.7...master)
+[Commits](https://github.com/kpdecker/jsdiff/compare/v1.0.8...master)
+
+## v1.0.8 - December 22nd, 2013
+- [#24](https://github.com/kpdecker/jsdiff/pull/24) - Handle windows newlines on non windows machines. ([@benogle](https://api.github.com/users/benogle))
+- [#23](https://github.com/kpdecker/jsdiff/pull/23) - Prettied up the API formatting a little, and added basic node and web examples ([@airportyh](https://api.github.com/users/airportyh))
+
+[Commits](https://github.com/kpdecker/jsdiff/compare/v1.0.7...v1.0.8)
## v1.0.7 - September 11th, 2013
diff --git a/test/diffTest.js b/test/diffTest.js
index 91dafe1c..e2aa8eab 100644
--- a/test/diffTest.js
+++ b/test/diffTest.js
@@ -103,6 +103,13 @@ describe('#diffLines', function() {
'line\nvalue\nline');
diff.convertChangesToXML(diffResult).should.equal('line\nvalue\nvalue \nline');
});
+
+ it('should handle windows line endings', function() {
+ var diffResult = diff.diffLines(
+ 'line\r\nold value \r\nline',
+ 'line\r\nnew value\r\nline');
+ diff.convertChangesToXML(diffResult).should.equal('line\r\nnew value\r\nold value \r\nline');
+ });
});
describe('convertToDMP', function() {