Skip to content

Commit

Permalink
Special case handling of ignored terminals
Browse files Browse the repository at this point in the history
Fixes #70
  • Loading branch information
kpdecker committed Aug 26, 2015
1 parent 542063c commit da522e3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/diff/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Diff.prototype = {

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

function buildValues(components, newString, oldString, useLongestToken) {
function buildValues(diff, components, newString, oldString, useLongestToken) {
let componentPos = 0,
componentLen = components.length,
newPos = 0,
Expand Down Expand Up @@ -197,6 +197,14 @@ function buildValues(components, newString, oldString, useLongestToken) {
}
}

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

return components;
}

Expand Down
8 changes: 8 additions & 0 deletions test/diff/word.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ describe('WordDiff', function() {
expect(diffWords('', 'foo')).to.eql([{value: 'foo', count: 1, added: true, removed: undefined}]);
expect(diffWords('', 'foo bar')).to.eql([{value: 'foo bar', count: 3, added: true, removed: undefined}]);
});

it('should ignore whitespace', function() {
expect(diffWords('hase igel fuchs', 'hase igel fuchs')).to.eql([{ count: 5, value: 'hase igel fuchs' }]);
expect(diffWords('hase igel fuchs', 'hase igel fuchs\n')).to.eql([{ count: 5, value: 'hase igel fuchs\n' }]);
expect(diffWords('hase igel fuchs\n', 'hase igel fuchs')).to.eql([{ count: 5, value: 'hase igel fuchs\n' }]);
expect(diffWords('hase igel fuchs', 'hase igel\nfuchs')).to.eql([{ count: 5, value: 'hase igel\nfuchs' }]);
expect(diffWords('hase igel\nfuchs', 'hase igel fuchs')).to.eql([{ count: 5, value: 'hase igel fuchs' }]);
});
});

describe('#diffWords - async', function() {
Expand Down

0 comments on commit da522e3

Please sign in to comment.