Skip to content

Commit cf6f569

Browse files
committed
Remove diff edge case special cases
These should still fail quickly and by not short circuiting this we can include information like the token count in the response. Fixes #69
1 parent 11c5b52 commit cf6f569

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

Diff for: src/diff/base.js

+2-13
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,8 @@ Diff.prototype = {
1919
oldString = this.castInput(oldString);
2020
newString = this.castInput(newString);
2121

22-
// Handle the identity case (this is due to unrolling editLength == 0
23-
if (newString === oldString) {
24-
return done([{ value: newString }]);
25-
}
26-
if (!newString) {
27-
return done([{ value: oldString, removed: true }]);
28-
}
29-
if (!oldString) {
30-
return done([{ value: newString, added: true }]);
31-
}
32-
33-
newString = this.removeEmpty(this.tokenize(newString));
3422
oldString = this.removeEmpty(this.tokenize(oldString));
23+
newString = this.removeEmpty(this.tokenize(newString));
3524

3625
let newLen = newString.length, oldLen = oldString.length;
3726
let editLength = 1;
@@ -42,7 +31,7 @@ Diff.prototype = {
4231
let oldPos = this.extractCommon(bestPath[0], newString, oldString, 0);
4332
if (bestPath[0].newPos + 1 >= newLen && oldPos + 1 >= oldLen) {
4433
// Identity per the equality and tokenizer
45-
return done([{value: newString.join('')}]);
34+
return done([{value: newString.join(''), count: newString.length}]);
4635
}
4736

4837
// Main worker method. checks all permutations of a given edit length for acceptance.

Diff for: test/diff/word.js

+12
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ describe('WordDiff', function() {
6565
expect(wordDiff.removeEmpty(wordDiff.tokenize('jurídica'))).to.eql(['jurídica']);
6666
expect(wordDiff.removeEmpty(wordDiff.tokenize('wir üben'))).to.eql(['wir', ' ', 'üben']);
6767
});
68+
69+
it('should include count with identity cases', function() {
70+
expect(diffWords('foo', 'foo')).to.eql([{value: 'foo', count: 1}]);
71+
expect(diffWords('foo bar', 'foo bar')).to.eql([{value: 'foo bar', count: 3}]);
72+
});
73+
it('should include count with empty cases', function() {
74+
expect(diffWords('foo', '')).to.eql([{value: 'foo', count: 1, added: undefined, removed: true}]);
75+
expect(diffWords('foo bar', '')).to.eql([{value: 'foo bar', count: 3, added: undefined, removed: true}]);
76+
77+
expect(diffWords('', 'foo')).to.eql([{value: 'foo', count: 1, added: true, removed: undefined}]);
78+
expect(diffWords('', 'foo bar')).to.eql([{value: 'foo bar', count: 3, added: true, removed: undefined}]);
79+
});
6880
});
6981

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

0 commit comments

Comments
 (0)