Skip to content
This repository was archived by the owner on Jan 14, 2019. It is now read-only.

Commit 3c78fd0

Browse files
artemvkpdecker
authored andcommitted
fix: some corrections re ignoreCase option
.. to make existing tests pass also added couple tests for ignoreCase option (but it's < 100% coverage still) + docs
1 parent 3cbfbb5 commit 3c78fd0

File tree

6 files changed

+57
-9
lines changed

6 files changed

+57
-9
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@ bower install jsdiff --save
2323

2424
Returns a list of change objects (See below).
2525

26+
Options
27+
* `ignoreCase`: `true` to ignore casing difference. Defaults to `false`.
28+
2629
* `JsDiff.diffWords(oldStr, newStr[, options])` - diffs two blocks of text, comparing word by word, ignoring whitespace.
2730

2831
Returns a list of change objects (See below).
2932

33+
Options
34+
* `ignoreCase`: Same as in `diffChars`.
35+
3036
* `JsDiff.diffWordsWithSpace(oldStr, newStr[, options])` - diffs two blocks of text, comparing word by word, treating whitespace as significant.
3137

3238
Returns a list of change objects (See below).

src/diff/character.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import Diff from './base';
22

33
export const characterDiff = new Diff();
4-
export function diffChars(oldStr, newStr, callback) { return characterDiff.diff(oldStr, newStr, callback); }
4+
export function diffChars(oldStr, newStr, options) { return characterDiff.diff(oldStr, newStr, options); }

src/diff/json.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jsonDiff.castInput = function(value) {
2222
}, ' ');
2323
};
2424
jsonDiff.equals = function(left, right) {
25-
return Diff.prototype.equals(left.replace(/,([\r\n])/g, '$1'), right.replace(/,([\r\n])/g, '$1'));
25+
return Diff.prototype.equals.call(jsonDiff, left.replace(/,([\r\n])/g, '$1'), right.replace(/,([\r\n])/g, '$1'));
2626
};
2727

2828
export function diffJson(oldObj, newObj, options) { return jsonDiff.diff(oldObj, newObj, options); }

src/diff/word.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,26 @@ const reWhitespace = /\S/;
2525

2626
export const wordDiff = new Diff();
2727
wordDiff.equals = function(left, right) {
28-
return (left === right)
29-
|| (this.options.ignoreWhitespace && !reWhitespace.test(left) && !reWhitespace.test(right))
30-
|| (this.options.ignoreCase && left.toLowerCase() === right.toLowerCase());
28+
if (left === right) {
29+
return true;
30+
}
31+
32+
let ignoreWhitespaceComparator = (left, right) => {
33+
return this.options.ignoreWhitespace && !reWhitespace.test(left) && !reWhitespace.test(right);
34+
};
35+
36+
if (this.options.ignoreCase) {
37+
left = left.toLowerCase();
38+
right = right.toLowerCase();
39+
if (left === right) {
40+
return true;
41+
}
42+
if (ignoreWhitespaceComparator(left, right)) {
43+
return true;
44+
}
45+
} else {
46+
return ignoreWhitespaceComparator(left, right);
47+
}
3148
};
3249
wordDiff.tokenize = function(value) {
3350
let tokens = value.split(/(\s+|\b)/);
@@ -47,10 +64,11 @@ wordDiff.tokenize = function(value) {
4764
return tokens;
4865
};
4966

50-
export function diffWords(oldStr, newStr, callback) {
51-
let options = generateOptions(callback, {ignoreWhitespace: true});
67+
export function diffWords(oldStr, newStr, options) {
68+
options = generateOptions(options, {ignoreWhitespace: true});
5269
return wordDiff.diff(oldStr, newStr, options);
5370
}
54-
export function diffWordsWithSpace(oldStr, newStr, callback) {
55-
return wordDiff.diff(oldStr, newStr, callback);
71+
72+
export function diffWordsWithSpace(oldStr, newStr, options) {
73+
return wordDiff.diff(oldStr, newStr, options);
5674
}

test/diff/character.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,17 @@ describe('diff/character', function() {
99
const diffResult = diffChars('New Value.', 'New ValueMoreData.');
1010
expect(convertChangesToXML(diffResult)).to.equal('New Value<ins>MoreData</ins>.');
1111
});
12+
13+
describe('case insensitivity', function() {
14+
it("is considered when there's no difference", function() {
15+
const diffResult = diffChars('New Value.', 'New value.', {ignoreCase: true});
16+
expect(convertChangesToXML(diffResult)).to.equal('New value.');
17+
});
18+
19+
it("is considered when there's a difference", function() {
20+
const diffResult = diffChars('New Values.', 'New value.', {ignoreCase: true});
21+
expect(convertChangesToXML(diffResult)).to.equal('New value<del>s</del>.');
22+
});
23+
});
1224
});
1325
});

test/diff/word.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,5 +190,17 @@ describe('WordDiff', function() {
190190
done();
191191
});
192192
});
193+
194+
describe('case insensitivity', function() {
195+
it("is considered when there's a difference", function() {
196+
const diffResult = diffWordsWithSpace('new value', 'New ValueMoreData', {ignoreCase: true});
197+
expect(convertChangesToXML(diffResult)).to.equal('New<del> value</del><ins> ValueMoreData</ins>');
198+
});
199+
200+
it("is considered when there's no difference", function() {
201+
const diffResult = diffWordsWithSpace('new value', 'New Value', {ignoreCase: true});
202+
expect(convertChangesToXML(diffResult)).to.equal('New Value');
203+
});
204+
});
193205
});
194206
});

0 commit comments

Comments
 (0)