Skip to content

Adding Diff Trimmed Lines #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 1, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.4.6927
*/
(function(global, undefined) {

var JsDiff = (function() {
/*jshint maxparams: 5*/
/*istanbul ignore next*/
Expand Down Expand Up @@ -114,8 +115,8 @@
return done([{ value: newString, added: true }]);
}

newString = this.tokenize(newString);
oldString = this.tokenize(oldString);
newString = this.tokenize(newString, !!self.ignoreTrim);
oldString = this.tokenize(oldString, !!self.ignoreTrim);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than changing the tokenize API, can't we just read this flag below?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, certainly, thanks for the line note. I worked myself into a logical corner, and couldn't think of any other way to do that when I was coding, so I did this hesitantly. Looking at it again, the correct solution is quite clear.


var newLen = newString.length, oldLen = oldString.length;
var maxEditLength = newLen + oldLen;
Expand Down Expand Up @@ -174,7 +175,7 @@
editLength++;
}

// Performs the length of edit iteration. Is a bit fugly as this has to support the
// Performs the length of edit iteration. Is a bit fugly as this has to support the
// sync and async mode which is never fun. Loops over execEditLength until a value
// is produced.
var editLength = 1;
Expand Down Expand Up @@ -256,7 +257,11 @@
};

var LineDiff = new Diff();
LineDiff.tokenize = function(value) {

var TrimmedLineDiff = new Diff();
TrimmedLineDiff.ignoreTrim = true;

LineDiff.tokenize = TrimmedLineDiff.tokenize = function(value, ignoreTrim) {
var retLines = [],
lines = value.split(/^/m);

Expand All @@ -268,13 +273,14 @@
if (line === '\n' && lastLine && lastLine[lastLine.length - 1] === '\r') {
retLines[retLines.length - 1] += '\n';
} else if (line) {
retLines.push(line);
retLines.push(ignoreTrim ? line.trim() + '\n' : line);
}
}

return retLines;
};


var SentenceDiff = new Diff();
SentenceDiff.tokenize = function (value) {
return removeEmpty(value.split(/(\S.+?[.!?])(?=\s+|$)/));
Expand Down Expand Up @@ -344,6 +350,8 @@
diffWords: function(oldStr, newStr, callback) { return WordDiff.diff(oldStr, newStr, callback); },
diffWordsWithSpace: function(oldStr, newStr, callback) { return WordWithSpaceDiff.diff(oldStr, newStr, callback); },
diffLines: function(oldStr, newStr, callback) { return LineDiff.diff(oldStr, newStr, callback); },
diffTrimmedLines: function(oldStr, newStr, callback) { return TrimmedLineDiff.diff(oldStr, newStr, callback); },

diffSentences: function(oldStr, newStr, callback) { return SentenceDiff.diff(oldStr, newStr, callback); },

diffCss: function(oldStr, newStr, callback) { return CssDiff.diff(oldStr, newStr, callback); },
Expand Down
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ <h1>Diff</h1>
<label><input type="radio" name="diff_type" value="diffChars" checked> Chars</label>
<label><input type="radio" name="diff_type" value="diffWords"> Words</label>
<label><input type="radio" name="diff_type" value="diffLines"> Lines</label>
<label><input type="radio" name="diff_type" value="diffTrimmedLines"> Trimmed Lines</label>
<label><input type="radio" name="diff_type" value="createPatch"> Patch</label>
<label><input type="radio" name="diff_type" value="applyPatch"> Merge</label>
</div>
Expand Down