Skip to content

Commit 9c802f7

Browse files
committed
Add support for sentences.
1 parent c178175 commit 9c802f7

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

Diff for: diff.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,19 @@
196196
return retLines;
197197
};
198198

199-
return {
200-
Diff: Diff,
199+
var SentenceDiff = new Diff();
200+
SentenceDiff.tokenize = function (value) {
201+
return removeEmpty(value.split(/(\S.+?[.!?])(?=\s+|$)/));
202+
};
203+
204+
return {
205+
Diff: Diff,
201206

202207
diffChars: function(oldStr, newStr) { return CharDiff.diff(oldStr, newStr); },
203208
diffWords: function(oldStr, newStr) { return WordDiff.diff(oldStr, newStr); },
204209
diffWordsWithSpace: function(oldStr, newStr) { return WordWithSpaceDiff.diff(oldStr, newStr); },
205210
diffLines: function(oldStr, newStr) { return LineDiff.diff(oldStr, newStr); },
211+
diffSentences: function(oldStr, newStr) { return SentenceDiff.diff(oldStr, newStr); },
206212

207213
diffCss: function(oldStr, newStr) { return CssDiff.diff(oldStr, newStr); },
208214

Diff for: test/diffTest.js

+13
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ describe('#diffWordsWithSpace', function() {
6969
});
7070
});
7171

72+
describe('#diffSentences', function() {
73+
74+
it('Should diff Sentences', function() {
75+
var diffResult = diff.diffSentences('New Value.', 'New ValueMoreData.');
76+
diff.convertChangesToXML(diffResult).should.equal('<ins>New ValueMoreData.</ins><del>New Value.</del>');
77+
});
78+
79+
it('should diff only the last sentence', function() {
80+
var diffResult = diff.diffSentences('Here im. Rock you like old man.', 'Here im. Rock you like hurricane.');
81+
diff.convertChangesToXML(diffResult).should.equal('Here im. <ins>Rock you like hurricane.</ins><del>Rock you like old man.</del>');
82+
});
83+
});
84+
7285
// CSS Diff
7386
describe('#diffCss', function() {
7487
it('should diff css', function() {

0 commit comments

Comments
 (0)