-
Notifications
You must be signed in to change notification settings - Fork 510
/
Copy pathword.js
241 lines (207 loc) · 10.6 KB
/
word.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import {wordDiff, diffWords, diffWordsWithSpace} from '../../lib/diff/word';
import {convertChangesToXML} from '../../lib/convert/xml';
import {expect} from 'chai';
describe('WordDiff', function() {
describe('#diffWords', function() {
it('should diff whitespace', function() {
const diffResult = diffWords('New Value', 'New ValueMoreData');
expect(convertChangesToXML(diffResult)).to.equal('New <del>Value</del><ins>ValueMoreData</ins>');
});
it('should diff multiple whitespace values', function() {
const diffResult = diffWords('New Value ', 'New ValueMoreData ');
expect(convertChangesToXML(diffResult)).to.equal('New <del>Value</del><ins>ValueMoreData</ins> ');
});
// Diff on word boundary
it('should diff on word boundaries', function() {
let diffResult = diffWords('New :Value:Test', 'New ValueMoreData ');
expect(convertChangesToXML(diffResult)).to.equal('New <del>:Value:Test</del><ins>ValueMoreData </ins>');
diffResult = diffWords('New Value:Test', 'New Value:MoreData ');
expect(convertChangesToXML(diffResult)).to.equal('New Value:<del>Test</del><ins>MoreData </ins>');
diffResult = diffWords('New Value-Test', 'New Value:MoreData ');
expect(convertChangesToXML(diffResult)).to.equal('New Value<del>-Test</del><ins>:MoreData </ins>');
diffResult = diffWords('New Value', 'New Value:MoreData ');
expect(convertChangesToXML(diffResult)).to.equal('New Value<ins>:MoreData </ins>');
});
// Diff without changes
it('should handle identity', function() {
const diffResult = diffWords('New Value', 'New Value');
expect(convertChangesToXML(diffResult)).to.equal('New Value');
});
it('should handle empty', function() {
const diffResult = diffWords('', '');
expect(convertChangesToXML(diffResult)).to.equal('');
});
it('should diff has identical content', function() {
const diffResult = diffWords('New Value', 'New Value');
expect(convertChangesToXML(diffResult)).to.equal('New Value');
});
// Empty diffs
it('should diff empty new content', function() {
const diffResult = diffWords('New Value', '');
expect(diffResult.length).to.equal(1);
expect(convertChangesToXML(diffResult)).to.equal('<del>New Value</del>');
});
it('should diff empty old content', function() {
const diffResult = diffWords('', 'New Value');
expect(convertChangesToXML(diffResult)).to.equal('<ins>New Value</ins>');
});
// With without anchor (the Heckel algorithm error case)
it('should diff when there is no anchor value', function() {
const diffResult = diffWords('New Value New Value', 'Value Value New New');
expect(convertChangesToXML(diffResult)).to.equal('<del>New</del><ins>Value</ins> Value New <del>Value</del><ins>New</ins>');
});
it('should token unicode characters safely', function() {
expect(wordDiff.removeEmpty(wordDiff.tokenize('jurídica'))).to.eql(['jurídica']);
expect(wordDiff.removeEmpty(wordDiff.tokenize('wir üben'))).to.eql(['wir', ' ', 'üben']);
expect(wordDiff.removeEmpty(wordDiff.tokenize('안녕.'))).to.eql(['안녕', '.']);
});
it('should include count with identity cases', function() {
expect(diffWords('foo', 'foo')).to.eql([{value: 'foo', count: 1}]);
expect(diffWords('foo bar', 'foo bar')).to.eql([{value: 'foo bar', count: 3}]);
});
it('should include count with empty cases', function() {
expect(diffWords('foo', '')).to.eql([{value: 'foo', count: 1, added: undefined, removed: true}]);
expect(diffWords('foo bar', '')).to.eql([{value: 'foo bar', count: 3, added: undefined, removed: true}]);
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' }]);
});
it('should diff whitespace with flag', function() {
const diffResult = diffWords('New Value', 'New ValueMoreData', {ignoreWhitespace: false});
expect(convertChangesToXML(diffResult)).to.equal('New<del> Value</del><ins> ValueMoreData</ins>');
});
it('should diff with only whitespace', function() {
let diffResult = diffWords('', ' ');
expect(convertChangesToXML(diffResult)).to.equal('<ins> </ins>');
diffResult = diffWords(' ', '');
expect(convertChangesToXML(diffResult)).to.equal('<del> </del>');
});
});
describe('#diffWords - async', function() {
it('should diff whitespace', function(done) {
diffWords('New Value', 'New ValueMoreData', function(err, diffResult) {
expect(err).to.be.undefined;
expect(convertChangesToXML(diffResult)).to.equal('New <del>Value</del><ins>ValueMoreData</ins>');
done();
});
});
it('should diff multiple whitespace values', function(done) {
diffWords('New Value ', 'New ValueMoreData ', function(err, diffResult) {
expect(err).to.be.undefined;
expect(convertChangesToXML(diffResult)).to.equal('New <del>Value</del><ins>ValueMoreData</ins> ');
done();
});
});
// Diff on word boundary
it('should diff on word boundaries', function(done) {
diffWords('New :Value:Test', 'New ValueMoreData ', function(err, diffResult) {
expect(err).to.be.undefined;
expect(convertChangesToXML(diffResult)).to.equal('New <del>:Value:Test</del><ins>ValueMoreData </ins>');
done();
});
});
// Diff without changes
it('should handle identity', function(done) {
diffWords('New Value', 'New Value', function(err, diffResult) {
expect(err).to.be.undefined;
expect(convertChangesToXML(diffResult)).to.equal('New Value');
done();
});
});
it('should handle empty', function(done) {
diffWords('', '', function(err, diffResult) {
expect(err).to.be.undefined;
expect(convertChangesToXML(diffResult)).to.equal('');
done();
});
});
it('should diff has identical content', function(done) {
diffWords('New Value', 'New Value', function(err, diffResult) {
expect(err).to.be.undefined;
expect(convertChangesToXML(diffResult)).to.equal('New Value');
done();
});
});
// Empty diffs
it('should diff empty new content', function(done) {
diffWords('New Value', '', function(err, diffResult) {
expect(diffResult.length).to.equal(1);
expect(convertChangesToXML(diffResult)).to.equal('<del>New Value</del>');
done();
});
});
it('should diff empty old content', function(done) {
diffWords('', 'New Value', function(err, diffResult) {
expect(convertChangesToXML(diffResult)).to.equal('<ins>New Value</ins>');
done();
});
});
// With without anchor (the Heckel algorithm error case)
it('should diff when there is no anchor value', function(done) {
diffWords('New Value New Value', 'Value Value New New', function(err, diffResult) {
expect(convertChangesToXML(diffResult)).to.equal('<del>New</del><ins>Value</ins> Value New <del>Value</del><ins>New</ins>');
done();
});
});
});
describe('#diffWordsWithSpace', function() {
it('should diff whitespace', function() {
const diffResult = diffWordsWithSpace('New Value', 'New ValueMoreData');
expect(convertChangesToXML(diffResult)).to.equal('New<del> Value</del><ins> ValueMoreData</ins>');
});
it('should diff multiple whitespace values', function() {
const diffResult = diffWordsWithSpace('New Value ', 'New ValueMoreData ');
expect(convertChangesToXML(diffResult)).to.equal('New<ins> ValueMoreData</ins> <del>Value </del>');
});
it('should inserts values in parenthesis', function() {
const diffResult = diffWordsWithSpace('()', '(word)');
expect(convertChangesToXML(diffResult)).to.equal('(<ins>word</ins>)');
});
it('should inserts values in brackets', function() {
const diffResult = diffWordsWithSpace('[]', '[word]');
expect(convertChangesToXML(diffResult)).to.equal('[<ins>word</ins>]');
});
it('should inserts values in curly braces', function() {
const diffResult = diffWordsWithSpace('{}', '{word}');
expect(convertChangesToXML(diffResult)).to.equal('{<ins>word</ins>}');
});
it('should inserts values in quotes', function() {
const diffResult = diffWordsWithSpace("''", "'word'");
expect(convertChangesToXML(diffResult)).to.equal("'<ins>word</ins>'");
});
it('should inserts values in double quotes', function() {
const diffResult = diffWordsWithSpace('""', '"word"');
expect(convertChangesToXML(diffResult)).to.equal('"<ins>word</ins>"');
});
it('should threat newline as separate token (issues #180, #211)', function() {
// #180
const diffResult1 = diffWordsWithSpace('foo\nbar', 'foo\n\n\nbar');
expect(convertChangesToXML(diffResult1)).to.equal('foo\n<ins>\n\n</ins>bar');
// #211
const diffResult2 = diffWordsWithSpace('A\n\nB\n', 'A\nB\n');
expect(convertChangesToXML(diffResult2)).to.equal('A\n<del>\n</del>B\n');
});
it('should perform async operations', function(done) {
diffWordsWithSpace('New Value ', 'New ValueMoreData ', function(err, diffResult) {
expect(convertChangesToXML(diffResult)).to.equal('New<ins> ValueMoreData</ins> <del>Value </del>');
done();
});
});
describe('case insensitivity', function() {
it("is considered when there's a difference", function() {
const diffResult = diffWordsWithSpace('new value', 'New ValueMoreData', {ignoreCase: true});
expect(convertChangesToXML(diffResult)).to.equal('New<del> value</del><ins> ValueMoreData</ins>');
});
it("is considered when there's no difference", function() {
const diffResult = diffWordsWithSpace('new value', 'New Value', {ignoreCase: true});
expect(convertChangesToXML(diffResult)).to.equal('New Value');
});
});
});
});