Skip to content

Commit da522e3

Browse files
committed
Special case handling of ignored terminals
Fixes #70
1 parent 542063c commit da522e3

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/diff/base.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Diff.prototype = {
6868

6969
// If we have hit the end of both strings, then we are done
7070
if (basePath.newPos + 1 >= newLen && oldPos + 1 >= oldLen) {
71-
return done(buildValues(basePath.components, newString, oldString, self.useLongestToken));
71+
return done(buildValues(self, basePath.components, newString, oldString, self.useLongestToken));
7272
} else {
7373
// Otherwise track this path as a potential candidate and continue.
7474
bestPath[diagonalPath] = basePath;
@@ -156,7 +156,7 @@ Diff.prototype = {
156156
}
157157
};
158158

159-
function buildValues(components, newString, oldString, useLongestToken) {
159+
function buildValues(diff, components, newString, oldString, useLongestToken) {
160160
let componentPos = 0,
161161
componentLen = components.length,
162162
newPos = 0,
@@ -197,6 +197,14 @@ function buildValues(components, newString, oldString, useLongestToken) {
197197
}
198198
}
199199

200+
// Special case handle for when one terminal is ignored. For this case we merge the
201+
// terminal into the prior string and drop the change.
202+
let lastComponent = components[componentLen - 1];
203+
if ((lastComponent.added || lastComponent.removed) && diff.equals('', lastComponent.value)) {
204+
components[componentLen - 2].value += lastComponent.value;
205+
components.pop();
206+
}
207+
200208
return components;
201209
}
202210

test/diff/word.js

+8
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ describe('WordDiff', function() {
7777
expect(diffWords('', 'foo')).to.eql([{value: 'foo', count: 1, added: true, removed: undefined}]);
7878
expect(diffWords('', 'foo bar')).to.eql([{value: 'foo bar', count: 3, added: true, removed: undefined}]);
7979
});
80+
81+
it('should ignore whitespace', function() {
82+
expect(diffWords('hase igel fuchs', 'hase igel fuchs')).to.eql([{ count: 5, value: 'hase igel fuchs' }]);
83+
expect(diffWords('hase igel fuchs', 'hase igel fuchs\n')).to.eql([{ count: 5, value: 'hase igel fuchs\n' }]);
84+
expect(diffWords('hase igel fuchs\n', 'hase igel fuchs')).to.eql([{ count: 5, value: 'hase igel fuchs\n' }]);
85+
expect(diffWords('hase igel fuchs', 'hase igel\nfuchs')).to.eql([{ count: 5, value: 'hase igel\nfuchs' }]);
86+
expect(diffWords('hase igel\nfuchs', 'hase igel fuchs')).to.eql([{ count: 5, value: 'hase igel fuchs' }]);
87+
});
8088
});
8189

8290
describe('#diffWords - async', function() {

0 commit comments

Comments
 (0)