@vitest/utils 3.1.0-beta.1 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-_commonjsHelpers.js +138 -143
- package/dist/diff.js +1062 -1129
- package/dist/error.js +110 -117
- package/dist/helpers.d.ts +2 -1
- package/dist/helpers.js +199 -181
- package/dist/index.d.ts +2 -1
- package/dist/index.js +94 -114
- package/dist/source-map.d.ts +2 -1
- package/dist/source-map.js +169 -166
- package/package.json +3 -3
package/dist/diff.js
CHANGED
@@ -4,365 +4,428 @@ import { g as getDefaultExportFromCjs, s as stringify } from './chunk-_commonjsH
|
|
4
4
|
import { deepClone, getOwnProperties, getType as getType$1 } from './helpers.js';
|
5
5
|
import 'loupe';
|
6
6
|
|
7
|
+
/**
|
8
|
+
* Diff Match and Patch
|
9
|
+
* Copyright 2018 The diff-match-patch Authors.
|
10
|
+
* https://github.com/google/diff-match-patch
|
11
|
+
*
|
12
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
13
|
+
* you may not use this file except in compliance with the License.
|
14
|
+
* You may obtain a copy of the License at
|
15
|
+
*
|
16
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
17
|
+
*
|
18
|
+
* Unless required by applicable law or agreed to in writing, software
|
19
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
20
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
21
|
+
* See the License for the specific language governing permissions and
|
22
|
+
* limitations under the License.
|
23
|
+
*/
|
24
|
+
/**
|
25
|
+
* @fileoverview Computes the difference between two texts to create a patch.
|
26
|
+
* Applies the patch onto another text, allowing for errors.
|
27
|
+
* @author fraser@google.com (Neil Fraser)
|
28
|
+
*/
|
29
|
+
/**
|
30
|
+
* CHANGES by pedrottimark to diff_match_patch_uncompressed.ts file:
|
31
|
+
*
|
32
|
+
* 1. Delete anything not needed to use diff_cleanupSemantic method
|
33
|
+
* 2. Convert from prototype properties to var declarations
|
34
|
+
* 3. Convert Diff to class from constructor and prototype
|
35
|
+
* 4. Add type annotations for arguments and return values
|
36
|
+
* 5. Add exports
|
37
|
+
*/
|
38
|
+
/**
|
39
|
+
* The data structure representing a diff is an array of tuples:
|
40
|
+
* [[DIFF_DELETE, 'Hello'], [DIFF_INSERT, 'Goodbye'], [DIFF_EQUAL, ' world.']]
|
41
|
+
* which means: delete 'Hello', add 'Goodbye' and keep ' world.'
|
42
|
+
*/
|
7
43
|
const DIFF_DELETE = -1;
|
8
44
|
const DIFF_INSERT = 1;
|
9
45
|
const DIFF_EQUAL = 0;
|
46
|
+
/**
|
47
|
+
* Class representing one diff tuple.
|
48
|
+
* Attempts to look like a two-element array (which is what this used to be).
|
49
|
+
* @param {number} op Operation, one of: DIFF_DELETE, DIFF_INSERT, DIFF_EQUAL.
|
50
|
+
* @param {string} text Text to be deleted, inserted, or retained.
|
51
|
+
* @constructor
|
52
|
+
*/
|
10
53
|
class Diff {
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
54
|
+
0;
|
55
|
+
1;
|
56
|
+
constructor(op, text) {
|
57
|
+
this[0] = op;
|
58
|
+
this[1] = text;
|
59
|
+
}
|
17
60
|
}
|
61
|
+
/**
|
62
|
+
* Determine the common prefix of two strings.
|
63
|
+
* @param {string} text1 First string.
|
64
|
+
* @param {string} text2 Second string.
|
65
|
+
* @return {number} The number of characters common to the start of each
|
66
|
+
* string.
|
67
|
+
*/
|
18
68
|
function diff_commonPrefix(text1, text2) {
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
69
|
+
if (!text1 || !text2 || text1.charAt(0) !== text2.charAt(0)) {
|
70
|
+
return 0;
|
71
|
+
}
|
72
|
+
let pointermin = 0;
|
73
|
+
let pointermax = Math.min(text1.length, text2.length);
|
74
|
+
let pointermid = pointermax;
|
75
|
+
let pointerstart = 0;
|
76
|
+
while (pointermin < pointermid) {
|
77
|
+
if (text1.substring(pointerstart, pointermid) === text2.substring(pointerstart, pointermid)) {
|
78
|
+
pointermin = pointermid;
|
79
|
+
pointerstart = pointermin;
|
80
|
+
} else {
|
81
|
+
pointermax = pointermid;
|
82
|
+
}
|
83
|
+
pointermid = Math.floor((pointermax - pointermin) / 2 + pointermin);
|
84
|
+
}
|
85
|
+
return pointermid;
|
36
86
|
}
|
87
|
+
/**
|
88
|
+
* Determine the common suffix of two strings.
|
89
|
+
* @param {string} text1 First string.
|
90
|
+
* @param {string} text2 Second string.
|
91
|
+
* @return {number} The number of characters common to the end of each string.
|
92
|
+
*/
|
37
93
|
function diff_commonSuffix(text1, text2) {
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
94
|
+
if (!text1 || !text2 || text1.charAt(text1.length - 1) !== text2.charAt(text2.length - 1)) {
|
95
|
+
return 0;
|
96
|
+
}
|
97
|
+
let pointermin = 0;
|
98
|
+
let pointermax = Math.min(text1.length, text2.length);
|
99
|
+
let pointermid = pointermax;
|
100
|
+
let pointerend = 0;
|
101
|
+
while (pointermin < pointermid) {
|
102
|
+
if (text1.substring(text1.length - pointermid, text1.length - pointerend) === text2.substring(text2.length - pointermid, text2.length - pointerend)) {
|
103
|
+
pointermin = pointermid;
|
104
|
+
pointerend = pointermin;
|
105
|
+
} else {
|
106
|
+
pointermax = pointermid;
|
107
|
+
}
|
108
|
+
pointermid = Math.floor((pointermax - pointermin) / 2 + pointermin);
|
109
|
+
}
|
110
|
+
return pointermid;
|
55
111
|
}
|
112
|
+
/**
|
113
|
+
* Determine if the suffix of one string is the prefix of another.
|
114
|
+
* @param {string} text1 First string.
|
115
|
+
* @param {string} text2 Second string.
|
116
|
+
* @return {number} The number of characters common to the end of the first
|
117
|
+
* string and the start of the second string.
|
118
|
+
* @private
|
119
|
+
*/
|
56
120
|
function diff_commonOverlap_(text1, text2) {
|
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
|
-
|
121
|
+
const text1_length = text1.length;
|
122
|
+
const text2_length = text2.length;
|
123
|
+
if (text1_length === 0 || text2_length === 0) {
|
124
|
+
return 0;
|
125
|
+
}
|
126
|
+
if (text1_length > text2_length) {
|
127
|
+
text1 = text1.substring(text1_length - text2_length);
|
128
|
+
} else if (text1_length < text2_length) {
|
129
|
+
text2 = text2.substring(0, text1_length);
|
130
|
+
}
|
131
|
+
const text_length = Math.min(text1_length, text2_length);
|
132
|
+
if (text1 === text2) {
|
133
|
+
return text_length;
|
134
|
+
}
|
135
|
+
let best = 0;
|
136
|
+
let length = 1;
|
137
|
+
while (true) {
|
138
|
+
const pattern = text1.substring(text_length - length);
|
139
|
+
const found = text2.indexOf(pattern);
|
140
|
+
if (found === -1) {
|
141
|
+
return best;
|
142
|
+
}
|
143
|
+
length += found;
|
144
|
+
if (found === 0 || text1.substring(text_length - length) === text2.substring(0, length)) {
|
145
|
+
best = length;
|
146
|
+
length++;
|
147
|
+
}
|
148
|
+
}
|
85
149
|
}
|
150
|
+
/**
|
151
|
+
* Reduce the number of edits by eliminating semantically trivial equalities.
|
152
|
+
* @param {!Array.<!diff_match_patch.Diff>} diffs Array of diff tuples.
|
153
|
+
*/
|
86
154
|
function diff_cleanupSemantic(diffs) {
|
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
|
-
0,
|
160
|
-
new Diff(DIFF_EQUAL, deletion.substring(0, overlap_length2))
|
161
|
-
);
|
162
|
-
diffs[pointer - 1][0] = DIFF_INSERT;
|
163
|
-
diffs[pointer - 1][1] = insertion.substring(
|
164
|
-
0,
|
165
|
-
insertion.length - overlap_length2
|
166
|
-
);
|
167
|
-
diffs[pointer + 1][0] = DIFF_DELETE;
|
168
|
-
diffs[pointer + 1][1] = deletion.substring(overlap_length2);
|
169
|
-
pointer++;
|
170
|
-
}
|
171
|
-
}
|
172
|
-
pointer++;
|
173
|
-
}
|
174
|
-
pointer++;
|
175
|
-
}
|
155
|
+
let changes = false;
|
156
|
+
const equalities = [];
|
157
|
+
let equalitiesLength = 0;
|
158
|
+
/** @type {?string} */
|
159
|
+
let lastEquality = null;
|
160
|
+
let pointer = 0;
|
161
|
+
let length_insertions1 = 0;
|
162
|
+
let length_deletions1 = 0;
|
163
|
+
let length_insertions2 = 0;
|
164
|
+
let length_deletions2 = 0;
|
165
|
+
while (pointer < diffs.length) {
|
166
|
+
if (diffs[pointer][0] === DIFF_EQUAL) {
|
167
|
+
equalities[equalitiesLength++] = pointer;
|
168
|
+
length_insertions1 = length_insertions2;
|
169
|
+
length_deletions1 = length_deletions2;
|
170
|
+
length_insertions2 = 0;
|
171
|
+
length_deletions2 = 0;
|
172
|
+
lastEquality = diffs[pointer][1];
|
173
|
+
} else {
|
174
|
+
if (diffs[pointer][0] === DIFF_INSERT) {
|
175
|
+
length_insertions2 += diffs[pointer][1].length;
|
176
|
+
} else {
|
177
|
+
length_deletions2 += diffs[pointer][1].length;
|
178
|
+
}
|
179
|
+
if (lastEquality && lastEquality.length <= Math.max(length_insertions1, length_deletions1) && lastEquality.length <= Math.max(length_insertions2, length_deletions2)) {
|
180
|
+
diffs.splice(equalities[equalitiesLength - 1], 0, new Diff(DIFF_DELETE, lastEquality));
|
181
|
+
diffs[equalities[equalitiesLength - 1] + 1][0] = DIFF_INSERT;
|
182
|
+
equalitiesLength--;
|
183
|
+
equalitiesLength--;
|
184
|
+
pointer = equalitiesLength > 0 ? equalities[equalitiesLength - 1] : -1;
|
185
|
+
length_insertions1 = 0;
|
186
|
+
length_deletions1 = 0;
|
187
|
+
length_insertions2 = 0;
|
188
|
+
length_deletions2 = 0;
|
189
|
+
lastEquality = null;
|
190
|
+
changes = true;
|
191
|
+
}
|
192
|
+
}
|
193
|
+
pointer++;
|
194
|
+
}
|
195
|
+
if (changes) {
|
196
|
+
diff_cleanupMerge(diffs);
|
197
|
+
}
|
198
|
+
diff_cleanupSemanticLossless(diffs);
|
199
|
+
pointer = 1;
|
200
|
+
while (pointer < diffs.length) {
|
201
|
+
if (diffs[pointer - 1][0] === DIFF_DELETE && diffs[pointer][0] === DIFF_INSERT) {
|
202
|
+
const deletion = diffs[pointer - 1][1];
|
203
|
+
const insertion = diffs[pointer][1];
|
204
|
+
const overlap_length1 = diff_commonOverlap_(deletion, insertion);
|
205
|
+
const overlap_length2 = diff_commonOverlap_(insertion, deletion);
|
206
|
+
if (overlap_length1 >= overlap_length2) {
|
207
|
+
if (overlap_length1 >= deletion.length / 2 || overlap_length1 >= insertion.length / 2) {
|
208
|
+
diffs.splice(pointer, 0, new Diff(DIFF_EQUAL, insertion.substring(0, overlap_length1)));
|
209
|
+
diffs[pointer - 1][1] = deletion.substring(0, deletion.length - overlap_length1);
|
210
|
+
diffs[pointer + 1][1] = insertion.substring(overlap_length1);
|
211
|
+
pointer++;
|
212
|
+
}
|
213
|
+
} else {
|
214
|
+
if (overlap_length2 >= deletion.length / 2 || overlap_length2 >= insertion.length / 2) {
|
215
|
+
diffs.splice(pointer, 0, new Diff(DIFF_EQUAL, deletion.substring(0, overlap_length2)));
|
216
|
+
diffs[pointer - 1][0] = DIFF_INSERT;
|
217
|
+
diffs[pointer - 1][1] = insertion.substring(0, insertion.length - overlap_length2);
|
218
|
+
diffs[pointer + 1][0] = DIFF_DELETE;
|
219
|
+
diffs[pointer + 1][1] = deletion.substring(overlap_length2);
|
220
|
+
pointer++;
|
221
|
+
}
|
222
|
+
}
|
223
|
+
pointer++;
|
224
|
+
}
|
225
|
+
pointer++;
|
226
|
+
}
|
176
227
|
}
|
177
228
|
const nonAlphaNumericRegex_ = /[^a-z0-9]/i;
|
178
229
|
const whitespaceRegex_ = /\s/;
|
179
230
|
const linebreakRegex_ = /[\r\n]/;
|
180
231
|
const blanklineEndRegex_ = /\n\r?\n$/;
|
181
232
|
const blanklineStartRegex_ = /^\r?\n\r?\n/;
|
233
|
+
/**
|
234
|
+
* Look for single edits surrounded on both sides by equalities
|
235
|
+
* which can be shifted sideways to align the edit to a word boundary.
|
236
|
+
* e.g: The c<ins>at c</ins>ame. -> The <ins>cat </ins>came.
|
237
|
+
* @param {!Array.<!diff_match_patch.Diff>} diffs Array of diff tuples.
|
238
|
+
*/
|
182
239
|
function diff_cleanupSemanticLossless(diffs) {
|
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
|
-
|
240
|
+
let pointer = 1;
|
241
|
+
while (pointer < diffs.length - 1) {
|
242
|
+
if (diffs[pointer - 1][0] === DIFF_EQUAL && diffs[pointer + 1][0] === DIFF_EQUAL) {
|
243
|
+
let equality1 = diffs[pointer - 1][1];
|
244
|
+
let edit = diffs[pointer][1];
|
245
|
+
let equality2 = diffs[pointer + 1][1];
|
246
|
+
const commonOffset = diff_commonSuffix(equality1, edit);
|
247
|
+
if (commonOffset) {
|
248
|
+
const commonString = edit.substring(edit.length - commonOffset);
|
249
|
+
equality1 = equality1.substring(0, equality1.length - commonOffset);
|
250
|
+
edit = commonString + edit.substring(0, edit.length - commonOffset);
|
251
|
+
equality2 = commonString + equality2;
|
252
|
+
}
|
253
|
+
let bestEquality1 = equality1;
|
254
|
+
let bestEdit = edit;
|
255
|
+
let bestEquality2 = equality2;
|
256
|
+
let bestScore = diff_cleanupSemanticScore_(equality1, edit) + diff_cleanupSemanticScore_(edit, equality2);
|
257
|
+
while (edit.charAt(0) === equality2.charAt(0)) {
|
258
|
+
equality1 += edit.charAt(0);
|
259
|
+
edit = edit.substring(1) + equality2.charAt(0);
|
260
|
+
equality2 = equality2.substring(1);
|
261
|
+
const score = diff_cleanupSemanticScore_(equality1, edit) + diff_cleanupSemanticScore_(edit, equality2);
|
262
|
+
if (score >= bestScore) {
|
263
|
+
bestScore = score;
|
264
|
+
bestEquality1 = equality1;
|
265
|
+
bestEdit = edit;
|
266
|
+
bestEquality2 = equality2;
|
267
|
+
}
|
268
|
+
}
|
269
|
+
if (diffs[pointer - 1][1] !== bestEquality1) {
|
270
|
+
if (bestEquality1) {
|
271
|
+
diffs[pointer - 1][1] = bestEquality1;
|
272
|
+
} else {
|
273
|
+
diffs.splice(pointer - 1, 1);
|
274
|
+
pointer--;
|
275
|
+
}
|
276
|
+
diffs[pointer][1] = bestEdit;
|
277
|
+
if (bestEquality2) {
|
278
|
+
diffs[pointer + 1][1] = bestEquality2;
|
279
|
+
} else {
|
280
|
+
diffs.splice(pointer + 1, 1);
|
281
|
+
pointer--;
|
282
|
+
}
|
283
|
+
}
|
284
|
+
}
|
285
|
+
pointer++;
|
286
|
+
}
|
230
287
|
}
|
288
|
+
/**
|
289
|
+
* Reorder and merge like edit sections. Merge equalities.
|
290
|
+
* Any edit section can move as long as it doesn't cross an equality.
|
291
|
+
* @param {!Array.<!diff_match_patch.Diff>} diffs Array of diff tuples.
|
292
|
+
*/
|
231
293
|
function diff_cleanupMerge(diffs) {
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
diffs[pointer + 1][1] = diffs[pointer - 1][1] + diffs[pointer + 1][1];
|
321
|
-
diffs.splice(pointer - 1, 1);
|
322
|
-
changes = true;
|
323
|
-
} else if (diffs[pointer][1].substring(0, diffs[pointer + 1][1].length) === diffs[pointer + 1][1]) {
|
324
|
-
diffs[pointer - 1][1] += diffs[pointer + 1][1];
|
325
|
-
diffs[pointer][1] = diffs[pointer][1].substring(diffs[pointer + 1][1].length) + diffs[pointer + 1][1];
|
326
|
-
diffs.splice(pointer + 1, 1);
|
327
|
-
changes = true;
|
328
|
-
}
|
329
|
-
}
|
330
|
-
pointer++;
|
331
|
-
}
|
332
|
-
if (changes) {
|
333
|
-
diff_cleanupMerge(diffs);
|
334
|
-
}
|
294
|
+
diffs.push(new Diff(DIFF_EQUAL, ""));
|
295
|
+
let pointer = 0;
|
296
|
+
let count_delete = 0;
|
297
|
+
let count_insert = 0;
|
298
|
+
let text_delete = "";
|
299
|
+
let text_insert = "";
|
300
|
+
let commonlength;
|
301
|
+
while (pointer < diffs.length) {
|
302
|
+
switch (diffs[pointer][0]) {
|
303
|
+
case DIFF_INSERT:
|
304
|
+
count_insert++;
|
305
|
+
text_insert += diffs[pointer][1];
|
306
|
+
pointer++;
|
307
|
+
break;
|
308
|
+
case DIFF_DELETE:
|
309
|
+
count_delete++;
|
310
|
+
text_delete += diffs[pointer][1];
|
311
|
+
pointer++;
|
312
|
+
break;
|
313
|
+
case DIFF_EQUAL:
|
314
|
+
if (count_delete + count_insert > 1) {
|
315
|
+
if (count_delete !== 0 && count_insert !== 0) {
|
316
|
+
commonlength = diff_commonPrefix(text_insert, text_delete);
|
317
|
+
if (commonlength !== 0) {
|
318
|
+
if (pointer - count_delete - count_insert > 0 && diffs[pointer - count_delete - count_insert - 1][0] === DIFF_EQUAL) {
|
319
|
+
diffs[pointer - count_delete - count_insert - 1][1] += text_insert.substring(0, commonlength);
|
320
|
+
} else {
|
321
|
+
diffs.splice(0, 0, new Diff(DIFF_EQUAL, text_insert.substring(0, commonlength)));
|
322
|
+
pointer++;
|
323
|
+
}
|
324
|
+
text_insert = text_insert.substring(commonlength);
|
325
|
+
text_delete = text_delete.substring(commonlength);
|
326
|
+
}
|
327
|
+
commonlength = diff_commonSuffix(text_insert, text_delete);
|
328
|
+
if (commonlength !== 0) {
|
329
|
+
diffs[pointer][1] = text_insert.substring(text_insert.length - commonlength) + diffs[pointer][1];
|
330
|
+
text_insert = text_insert.substring(0, text_insert.length - commonlength);
|
331
|
+
text_delete = text_delete.substring(0, text_delete.length - commonlength);
|
332
|
+
}
|
333
|
+
}
|
334
|
+
pointer -= count_delete + count_insert;
|
335
|
+
diffs.splice(pointer, count_delete + count_insert);
|
336
|
+
if (text_delete.length) {
|
337
|
+
diffs.splice(pointer, 0, new Diff(DIFF_DELETE, text_delete));
|
338
|
+
pointer++;
|
339
|
+
}
|
340
|
+
if (text_insert.length) {
|
341
|
+
diffs.splice(pointer, 0, new Diff(DIFF_INSERT, text_insert));
|
342
|
+
pointer++;
|
343
|
+
}
|
344
|
+
pointer++;
|
345
|
+
} else if (pointer !== 0 && diffs[pointer - 1][0] === DIFF_EQUAL) {
|
346
|
+
diffs[pointer - 1][1] += diffs[pointer][1];
|
347
|
+
diffs.splice(pointer, 1);
|
348
|
+
} else {
|
349
|
+
pointer++;
|
350
|
+
}
|
351
|
+
count_insert = 0;
|
352
|
+
count_delete = 0;
|
353
|
+
text_delete = "";
|
354
|
+
text_insert = "";
|
355
|
+
break;
|
356
|
+
}
|
357
|
+
}
|
358
|
+
if (diffs[diffs.length - 1][1] === "") {
|
359
|
+
diffs.pop();
|
360
|
+
}
|
361
|
+
let changes = false;
|
362
|
+
pointer = 1;
|
363
|
+
while (pointer < diffs.length - 1) {
|
364
|
+
if (diffs[pointer - 1][0] === DIFF_EQUAL && diffs[pointer + 1][0] === DIFF_EQUAL) {
|
365
|
+
if (diffs[pointer][1].substring(diffs[pointer][1].length - diffs[pointer - 1][1].length) === diffs[pointer - 1][1]) {
|
366
|
+
diffs[pointer][1] = diffs[pointer - 1][1] + diffs[pointer][1].substring(0, diffs[pointer][1].length - diffs[pointer - 1][1].length);
|
367
|
+
diffs[pointer + 1][1] = diffs[pointer - 1][1] + diffs[pointer + 1][1];
|
368
|
+
diffs.splice(pointer - 1, 1);
|
369
|
+
changes = true;
|
370
|
+
} else if (diffs[pointer][1].substring(0, diffs[pointer + 1][1].length) === diffs[pointer + 1][1]) {
|
371
|
+
diffs[pointer - 1][1] += diffs[pointer + 1][1];
|
372
|
+
diffs[pointer][1] = diffs[pointer][1].substring(diffs[pointer + 1][1].length) + diffs[pointer + 1][1];
|
373
|
+
diffs.splice(pointer + 1, 1);
|
374
|
+
changes = true;
|
375
|
+
}
|
376
|
+
}
|
377
|
+
pointer++;
|
378
|
+
}
|
379
|
+
if (changes) {
|
380
|
+
diff_cleanupMerge(diffs);
|
381
|
+
}
|
335
382
|
}
|
383
|
+
/**
|
384
|
+
* Given two strings, compute a score representing whether the internal
|
385
|
+
* boundary falls on logical boundaries.
|
386
|
+
* Scores range from 6 (best) to 0 (worst).
|
387
|
+
* Closure, but does not reference any external variables.
|
388
|
+
* @param {string} one First string.
|
389
|
+
* @param {string} two Second string.
|
390
|
+
* @return {number} The score.
|
391
|
+
* @private
|
392
|
+
*/
|
336
393
|
function diff_cleanupSemanticScore_(one, two) {
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
394
|
+
if (!one || !two) {
|
395
|
+
return 6;
|
396
|
+
}
|
397
|
+
const char1 = one.charAt(one.length - 1);
|
398
|
+
const char2 = two.charAt(0);
|
399
|
+
const nonAlphaNumeric1 = char1.match(nonAlphaNumericRegex_);
|
400
|
+
const nonAlphaNumeric2 = char2.match(nonAlphaNumericRegex_);
|
401
|
+
const whitespace1 = nonAlphaNumeric1 && char1.match(whitespaceRegex_);
|
402
|
+
const whitespace2 = nonAlphaNumeric2 && char2.match(whitespaceRegex_);
|
403
|
+
const lineBreak1 = whitespace1 && char1.match(linebreakRegex_);
|
404
|
+
const lineBreak2 = whitespace2 && char2.match(linebreakRegex_);
|
405
|
+
const blankLine1 = lineBreak1 && one.match(blanklineEndRegex_);
|
406
|
+
const blankLine2 = lineBreak2 && two.match(blanklineStartRegex_);
|
407
|
+
if (blankLine1 || blankLine2) {
|
408
|
+
return 5;
|
409
|
+
} else if (lineBreak1 || lineBreak2) {
|
410
|
+
return 4;
|
411
|
+
} else if (nonAlphaNumeric1 && !whitespace1 && whitespace2) {
|
412
|
+
return 3;
|
413
|
+
} else if (whitespace1 || whitespace2) {
|
414
|
+
return 2;
|
415
|
+
} else if (nonAlphaNumeric1 || nonAlphaNumeric2) {
|
416
|
+
return 1;
|
417
|
+
}
|
418
|
+
return 0;
|
362
419
|
}
|
363
420
|
|
421
|
+
/**
|
422
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
423
|
+
*
|
424
|
+
* This source code is licensed under the MIT license found in the
|
425
|
+
* LICENSE file in the root directory of this source tree.
|
426
|
+
*/
|
364
427
|
const NO_DIFF_MESSAGE = "Compared values have no visual difference.";
|
365
|
-
const SIMILAR_MESSAGE = "Compared values serialize to the same structure.\
|
428
|
+
const SIMILAR_MESSAGE = "Compared values serialize to the same structure.\n" + "Printing internal object structure without calling `toJSON` instead.";
|
366
429
|
|
367
430
|
var build = {};
|
368
431
|
|
@@ -1175,904 +1238,774 @@ var buildExports = requireBuild();
|
|
1175
1238
|
var diffSequences = /*@__PURE__*/getDefaultExportFromCjs(buildExports);
|
1176
1239
|
|
1177
1240
|
function formatTrailingSpaces(line, trailingSpaceFormatter) {
|
1178
|
-
|
1241
|
+
return line.replace(/\s+$/, (match) => trailingSpaceFormatter(match));
|
1179
1242
|
}
|
1180
1243
|
function printDiffLine(line, isFirstOrLast, color, indicator, trailingSpaceFormatter, emptyFirstOrLastLinePlaceholder) {
|
1181
|
-
|
1182
|
-
`${indicator} ${formatTrailingSpaces(line, trailingSpaceFormatter)}`
|
1183
|
-
) : indicator !== " " ? color(indicator) : isFirstOrLast && emptyFirstOrLastLinePlaceholder.length !== 0 ? color(`${indicator} ${emptyFirstOrLastLinePlaceholder}`) : "";
|
1244
|
+
return line.length !== 0 ? color(`${indicator} ${formatTrailingSpaces(line, trailingSpaceFormatter)}`) : indicator !== " " ? color(indicator) : isFirstOrLast && emptyFirstOrLastLinePlaceholder.length !== 0 ? color(`${indicator} ${emptyFirstOrLastLinePlaceholder}`) : "";
|
1184
1245
|
}
|
1185
|
-
function printDeleteLine(line, isFirstOrLast, {
|
1186
|
-
|
1187
|
-
aIndicator,
|
1188
|
-
changeLineTrailingSpaceColor,
|
1189
|
-
emptyFirstOrLastLinePlaceholder
|
1190
|
-
}) {
|
1191
|
-
return printDiffLine(
|
1192
|
-
line,
|
1193
|
-
isFirstOrLast,
|
1194
|
-
aColor,
|
1195
|
-
aIndicator,
|
1196
|
-
changeLineTrailingSpaceColor,
|
1197
|
-
emptyFirstOrLastLinePlaceholder
|
1198
|
-
);
|
1246
|
+
function printDeleteLine(line, isFirstOrLast, { aColor, aIndicator, changeLineTrailingSpaceColor, emptyFirstOrLastLinePlaceholder }) {
|
1247
|
+
return printDiffLine(line, isFirstOrLast, aColor, aIndicator, changeLineTrailingSpaceColor, emptyFirstOrLastLinePlaceholder);
|
1199
1248
|
}
|
1200
|
-
function printInsertLine(line, isFirstOrLast, {
|
1201
|
-
|
1202
|
-
bIndicator,
|
1203
|
-
changeLineTrailingSpaceColor,
|
1204
|
-
emptyFirstOrLastLinePlaceholder
|
1205
|
-
}) {
|
1206
|
-
return printDiffLine(
|
1207
|
-
line,
|
1208
|
-
isFirstOrLast,
|
1209
|
-
bColor,
|
1210
|
-
bIndicator,
|
1211
|
-
changeLineTrailingSpaceColor,
|
1212
|
-
emptyFirstOrLastLinePlaceholder
|
1213
|
-
);
|
1249
|
+
function printInsertLine(line, isFirstOrLast, { bColor, bIndicator, changeLineTrailingSpaceColor, emptyFirstOrLastLinePlaceholder }) {
|
1250
|
+
return printDiffLine(line, isFirstOrLast, bColor, bIndicator, changeLineTrailingSpaceColor, emptyFirstOrLastLinePlaceholder);
|
1214
1251
|
}
|
1215
|
-
function printCommonLine(line, isFirstOrLast, {
|
1216
|
-
|
1217
|
-
commonIndicator,
|
1218
|
-
commonLineTrailingSpaceColor,
|
1219
|
-
emptyFirstOrLastLinePlaceholder
|
1220
|
-
}) {
|
1221
|
-
return printDiffLine(
|
1222
|
-
line,
|
1223
|
-
isFirstOrLast,
|
1224
|
-
commonColor,
|
1225
|
-
commonIndicator,
|
1226
|
-
commonLineTrailingSpaceColor,
|
1227
|
-
emptyFirstOrLastLinePlaceholder
|
1228
|
-
);
|
1252
|
+
function printCommonLine(line, isFirstOrLast, { commonColor, commonIndicator, commonLineTrailingSpaceColor, emptyFirstOrLastLinePlaceholder }) {
|
1253
|
+
return printDiffLine(line, isFirstOrLast, commonColor, commonIndicator, commonLineTrailingSpaceColor, emptyFirstOrLastLinePlaceholder);
|
1229
1254
|
}
|
1230
1255
|
function createPatchMark(aStart, aEnd, bStart, bEnd, { patchColor }) {
|
1231
|
-
|
1232
|
-
`@@ -${aStart + 1},${aEnd - aStart} +${bStart + 1},${bEnd - bStart} @@`
|
1233
|
-
);
|
1256
|
+
return patchColor(`@@ -${aStart + 1},${aEnd - aStart} +${bStart + 1},${bEnd - bStart} @@`);
|
1234
1257
|
}
|
1235
1258
|
function joinAlignedDiffsNoExpand(diffs, options) {
|
1236
|
-
|
1237
|
-
|
1238
|
-
|
1239
|
-
|
1240
|
-
|
1241
|
-
|
1242
|
-
|
1243
|
-
|
1244
|
-
|
1245
|
-
|
1246
|
-
|
1247
|
-
|
1248
|
-
|
1249
|
-
|
1250
|
-
|
1251
|
-
|
1252
|
-
|
1253
|
-
|
1254
|
-
|
1255
|
-
|
1256
|
-
|
1257
|
-
|
1258
|
-
|
1259
|
-
|
1260
|
-
|
1261
|
-
|
1262
|
-
|
1263
|
-
|
1264
|
-
|
1265
|
-
|
1266
|
-
|
1267
|
-
|
1268
|
-
|
1269
|
-
|
1270
|
-
|
1271
|
-
|
1272
|
-
|
1273
|
-
|
1274
|
-
|
1275
|
-
|
1276
|
-
|
1277
|
-
|
1278
|
-
|
1279
|
-
|
1280
|
-
|
1281
|
-
|
1282
|
-
|
1283
|
-
|
1284
|
-
|
1285
|
-
|
1286
|
-
|
1287
|
-
|
1288
|
-
|
1289
|
-
|
1290
|
-
|
1291
|
-
|
1292
|
-
|
1293
|
-
|
1294
|
-
|
1295
|
-
|
1296
|
-
|
1297
|
-
|
1298
|
-
|
1299
|
-
|
1300
|
-
|
1301
|
-
|
1302
|
-
|
1303
|
-
|
1304
|
-
|
1305
|
-
|
1306
|
-
|
1307
|
-
|
1308
|
-
|
1309
|
-
|
1310
|
-
|
1311
|
-
|
1312
|
-
|
1313
|
-
|
1314
|
-
|
1315
|
-
|
1316
|
-
|
1317
|
-
|
1318
|
-
|
1319
|
-
|
1320
|
-
|
1321
|
-
|
1322
|
-
|
1323
|
-
|
1324
|
-
|
1325
|
-
|
1326
|
-
|
1327
|
-
|
1328
|
-
|
1329
|
-
|
1330
|
-
|
1331
|
-
|
1332
|
-
|
1333
|
-
|
1334
|
-
|
1335
|
-
|
1336
|
-
|
1337
|
-
|
1338
|
-
|
1339
|
-
|
1340
|
-
|
1341
|
-
|
1342
|
-
|
1343
|
-
|
1344
|
-
|
1345
|
-
|
1346
|
-
|
1347
|
-
|
1348
|
-
|
1349
|
-
|
1350
|
-
|
1351
|
-
|
1352
|
-
|
1353
|
-
|
1354
|
-
|
1355
|
-
|
1356
|
-
|
1357
|
-
|
1358
|
-
|
1359
|
-
|
1360
|
-
|
1361
|
-
|
1362
|
-
|
1363
|
-
|
1364
|
-
|
1365
|
-
}
|
1366
|
-
}
|
1367
|
-
if (hasPatch) {
|
1368
|
-
lines[jPatchMark] = createPatchMark(aStart, aEnd, bStart, bEnd, options);
|
1369
|
-
}
|
1370
|
-
return lines.join("\n");
|
1259
|
+
const iLength = diffs.length;
|
1260
|
+
const nContextLines = options.contextLines;
|
1261
|
+
const nContextLines2 = nContextLines + nContextLines;
|
1262
|
+
let jLength = iLength;
|
1263
|
+
let hasExcessAtStartOrEnd = false;
|
1264
|
+
let nExcessesBetweenChanges = 0;
|
1265
|
+
let i = 0;
|
1266
|
+
while (i !== iLength) {
|
1267
|
+
const iStart = i;
|
1268
|
+
while (i !== iLength && diffs[i][0] === DIFF_EQUAL) {
|
1269
|
+
i += 1;
|
1270
|
+
}
|
1271
|
+
if (iStart !== i) {
|
1272
|
+
if (iStart === 0) {
|
1273
|
+
if (i > nContextLines) {
|
1274
|
+
jLength -= i - nContextLines;
|
1275
|
+
hasExcessAtStartOrEnd = true;
|
1276
|
+
}
|
1277
|
+
} else if (i === iLength) {
|
1278
|
+
const n = i - iStart;
|
1279
|
+
if (n > nContextLines) {
|
1280
|
+
jLength -= n - nContextLines;
|
1281
|
+
hasExcessAtStartOrEnd = true;
|
1282
|
+
}
|
1283
|
+
} else {
|
1284
|
+
const n = i - iStart;
|
1285
|
+
if (n > nContextLines2) {
|
1286
|
+
jLength -= n - nContextLines2;
|
1287
|
+
nExcessesBetweenChanges += 1;
|
1288
|
+
}
|
1289
|
+
}
|
1290
|
+
}
|
1291
|
+
while (i !== iLength && diffs[i][0] !== DIFF_EQUAL) {
|
1292
|
+
i += 1;
|
1293
|
+
}
|
1294
|
+
}
|
1295
|
+
const hasPatch = nExcessesBetweenChanges !== 0 || hasExcessAtStartOrEnd;
|
1296
|
+
if (nExcessesBetweenChanges !== 0) {
|
1297
|
+
jLength += nExcessesBetweenChanges + 1;
|
1298
|
+
} else if (hasExcessAtStartOrEnd) {
|
1299
|
+
jLength += 1;
|
1300
|
+
}
|
1301
|
+
const jLast = jLength - 1;
|
1302
|
+
const lines = [];
|
1303
|
+
let jPatchMark = 0;
|
1304
|
+
if (hasPatch) {
|
1305
|
+
lines.push("");
|
1306
|
+
}
|
1307
|
+
let aStart = 0;
|
1308
|
+
let bStart = 0;
|
1309
|
+
let aEnd = 0;
|
1310
|
+
let bEnd = 0;
|
1311
|
+
const pushCommonLine = (line) => {
|
1312
|
+
const j = lines.length;
|
1313
|
+
lines.push(printCommonLine(line, j === 0 || j === jLast, options));
|
1314
|
+
aEnd += 1;
|
1315
|
+
bEnd += 1;
|
1316
|
+
};
|
1317
|
+
const pushDeleteLine = (line) => {
|
1318
|
+
const j = lines.length;
|
1319
|
+
lines.push(printDeleteLine(line, j === 0 || j === jLast, options));
|
1320
|
+
aEnd += 1;
|
1321
|
+
};
|
1322
|
+
const pushInsertLine = (line) => {
|
1323
|
+
const j = lines.length;
|
1324
|
+
lines.push(printInsertLine(line, j === 0 || j === jLast, options));
|
1325
|
+
bEnd += 1;
|
1326
|
+
};
|
1327
|
+
i = 0;
|
1328
|
+
while (i !== iLength) {
|
1329
|
+
let iStart = i;
|
1330
|
+
while (i !== iLength && diffs[i][0] === DIFF_EQUAL) {
|
1331
|
+
i += 1;
|
1332
|
+
}
|
1333
|
+
if (iStart !== i) {
|
1334
|
+
if (iStart === 0) {
|
1335
|
+
if (i > nContextLines) {
|
1336
|
+
iStart = i - nContextLines;
|
1337
|
+
aStart = iStart;
|
1338
|
+
bStart = iStart;
|
1339
|
+
aEnd = aStart;
|
1340
|
+
bEnd = bStart;
|
1341
|
+
}
|
1342
|
+
for (let iCommon = iStart; iCommon !== i; iCommon += 1) {
|
1343
|
+
pushCommonLine(diffs[iCommon][1]);
|
1344
|
+
}
|
1345
|
+
} else if (i === iLength) {
|
1346
|
+
const iEnd = i - iStart > nContextLines ? iStart + nContextLines : i;
|
1347
|
+
for (let iCommon = iStart; iCommon !== iEnd; iCommon += 1) {
|
1348
|
+
pushCommonLine(diffs[iCommon][1]);
|
1349
|
+
}
|
1350
|
+
} else {
|
1351
|
+
const nCommon = i - iStart;
|
1352
|
+
if (nCommon > nContextLines2) {
|
1353
|
+
const iEnd = iStart + nContextLines;
|
1354
|
+
for (let iCommon = iStart; iCommon !== iEnd; iCommon += 1) {
|
1355
|
+
pushCommonLine(diffs[iCommon][1]);
|
1356
|
+
}
|
1357
|
+
lines[jPatchMark] = createPatchMark(aStart, aEnd, bStart, bEnd, options);
|
1358
|
+
jPatchMark = lines.length;
|
1359
|
+
lines.push("");
|
1360
|
+
const nOmit = nCommon - nContextLines2;
|
1361
|
+
aStart = aEnd + nOmit;
|
1362
|
+
bStart = bEnd + nOmit;
|
1363
|
+
aEnd = aStart;
|
1364
|
+
bEnd = bStart;
|
1365
|
+
for (let iCommon = i - nContextLines; iCommon !== i; iCommon += 1) {
|
1366
|
+
pushCommonLine(diffs[iCommon][1]);
|
1367
|
+
}
|
1368
|
+
} else {
|
1369
|
+
for (let iCommon = iStart; iCommon !== i; iCommon += 1) {
|
1370
|
+
pushCommonLine(diffs[iCommon][1]);
|
1371
|
+
}
|
1372
|
+
}
|
1373
|
+
}
|
1374
|
+
}
|
1375
|
+
while (i !== iLength && diffs[i][0] === DIFF_DELETE) {
|
1376
|
+
pushDeleteLine(diffs[i][1]);
|
1377
|
+
i += 1;
|
1378
|
+
}
|
1379
|
+
while (i !== iLength && diffs[i][0] === DIFF_INSERT) {
|
1380
|
+
pushInsertLine(diffs[i][1]);
|
1381
|
+
i += 1;
|
1382
|
+
}
|
1383
|
+
}
|
1384
|
+
if (hasPatch) {
|
1385
|
+
lines[jPatchMark] = createPatchMark(aStart, aEnd, bStart, bEnd, options);
|
1386
|
+
}
|
1387
|
+
return lines.join("\n");
|
1371
1388
|
}
|
1372
1389
|
function joinAlignedDiffsExpand(diffs, options) {
|
1373
|
-
|
1374
|
-
|
1375
|
-
|
1376
|
-
|
1377
|
-
|
1378
|
-
|
1379
|
-
|
1380
|
-
|
1381
|
-
|
1382
|
-
return printCommonLine(line, isFirstOrLast, options);
|
1383
|
-
}
|
1384
|
-
}).join("\n");
|
1390
|
+
return diffs.map((diff, i, diffs) => {
|
1391
|
+
const line = diff[1];
|
1392
|
+
const isFirstOrLast = i === 0 || i === diffs.length - 1;
|
1393
|
+
switch (diff[0]) {
|
1394
|
+
case DIFF_DELETE: return printDeleteLine(line, isFirstOrLast, options);
|
1395
|
+
case DIFF_INSERT: return printInsertLine(line, isFirstOrLast, options);
|
1396
|
+
default: return printCommonLine(line, isFirstOrLast, options);
|
1397
|
+
}
|
1398
|
+
}).join("\n");
|
1385
1399
|
}
|
1386
1400
|
|
1387
1401
|
const noColor = (string) => string;
|
1388
1402
|
const DIFF_CONTEXT_DEFAULT = 5;
|
1389
1403
|
const DIFF_TRUNCATE_THRESHOLD_DEFAULT = 0;
|
1390
1404
|
function getDefaultOptions() {
|
1391
|
-
|
1392
|
-
|
1393
|
-
|
1394
|
-
|
1395
|
-
|
1396
|
-
|
1397
|
-
|
1398
|
-
|
1399
|
-
|
1400
|
-
|
1401
|
-
|
1402
|
-
|
1403
|
-
|
1404
|
-
|
1405
|
-
|
1406
|
-
|
1407
|
-
|
1408
|
-
|
1409
|
-
|
1410
|
-
|
1411
|
-
|
1412
|
-
|
1413
|
-
|
1414
|
-
|
1405
|
+
return {
|
1406
|
+
aAnnotation: "Expected",
|
1407
|
+
aColor: c.green,
|
1408
|
+
aIndicator: "-",
|
1409
|
+
bAnnotation: "Received",
|
1410
|
+
bColor: c.red,
|
1411
|
+
bIndicator: "+",
|
1412
|
+
changeColor: c.inverse,
|
1413
|
+
changeLineTrailingSpaceColor: noColor,
|
1414
|
+
commonColor: c.dim,
|
1415
|
+
commonIndicator: " ",
|
1416
|
+
commonLineTrailingSpaceColor: noColor,
|
1417
|
+
compareKeys: undefined,
|
1418
|
+
contextLines: DIFF_CONTEXT_DEFAULT,
|
1419
|
+
emptyFirstOrLastLinePlaceholder: "",
|
1420
|
+
expand: false,
|
1421
|
+
includeChangeCounts: false,
|
1422
|
+
omitAnnotationLines: false,
|
1423
|
+
patchColor: c.yellow,
|
1424
|
+
printBasicPrototype: false,
|
1425
|
+
truncateThreshold: DIFF_TRUNCATE_THRESHOLD_DEFAULT,
|
1426
|
+
truncateAnnotation: "... Diff result is truncated",
|
1427
|
+
truncateAnnotationColor: noColor
|
1428
|
+
};
|
1415
1429
|
}
|
1416
1430
|
function getCompareKeys(compareKeys) {
|
1417
|
-
|
1431
|
+
return compareKeys && typeof compareKeys === "function" ? compareKeys : undefined;
|
1418
1432
|
}
|
1419
1433
|
function getContextLines(contextLines) {
|
1420
|
-
|
1434
|
+
return typeof contextLines === "number" && Number.isSafeInteger(contextLines) && contextLines >= 0 ? contextLines : DIFF_CONTEXT_DEFAULT;
|
1421
1435
|
}
|
1422
1436
|
function normalizeDiffOptions(options = {}) {
|
1423
|
-
|
1424
|
-
|
1425
|
-
|
1426
|
-
|
1427
|
-
|
1428
|
-
|
1437
|
+
return {
|
1438
|
+
...getDefaultOptions(),
|
1439
|
+
...options,
|
1440
|
+
compareKeys: getCompareKeys(options.compareKeys),
|
1441
|
+
contextLines: getContextLines(options.contextLines)
|
1442
|
+
};
|
1429
1443
|
}
|
1430
1444
|
|
1431
1445
|
function isEmptyString(lines) {
|
1432
|
-
|
1446
|
+
return lines.length === 1 && lines[0].length === 0;
|
1433
1447
|
}
|
1434
1448
|
function countChanges(diffs) {
|
1435
|
-
|
1436
|
-
|
1437
|
-
|
1438
|
-
|
1439
|
-
|
1440
|
-
|
1441
|
-
|
1442
|
-
|
1443
|
-
|
1444
|
-
|
1445
|
-
|
1446
|
-
|
1447
|
-
|
1449
|
+
let a = 0;
|
1450
|
+
let b = 0;
|
1451
|
+
diffs.forEach((diff) => {
|
1452
|
+
switch (diff[0]) {
|
1453
|
+
case DIFF_DELETE:
|
1454
|
+
a += 1;
|
1455
|
+
break;
|
1456
|
+
case DIFF_INSERT:
|
1457
|
+
b += 1;
|
1458
|
+
break;
|
1459
|
+
}
|
1460
|
+
});
|
1461
|
+
return {
|
1462
|
+
a,
|
1463
|
+
b
|
1464
|
+
};
|
1448
1465
|
}
|
1449
|
-
function printAnnotation({
|
1450
|
-
|
1451
|
-
|
1452
|
-
|
1453
|
-
|
1454
|
-
|
1455
|
-
|
1456
|
-
|
1457
|
-
|
1458
|
-
|
1459
|
-
|
1460
|
-
|
1461
|
-
|
1462
|
-
|
1463
|
-
|
1464
|
-
|
1465
|
-
|
1466
|
-
|
1467
|
-
|
1468
|
-
|
1469
|
-
|
1470
|
-
const baCountLengthDiff = bCount.length - aCount.length;
|
1471
|
-
const aCountPadding = " ".repeat(Math.max(0, baCountLengthDiff));
|
1472
|
-
const bCountPadding = " ".repeat(Math.max(0, -baCountLengthDiff));
|
1473
|
-
aRest = `${aAnnotationPadding} ${aIndicator} ${aCountPadding}${aCount}`;
|
1474
|
-
bRest = `${bAnnotationPadding} ${bIndicator} ${bCountPadding}${bCount}`;
|
1475
|
-
}
|
1476
|
-
const a = `${aIndicator} ${aAnnotation}${aRest}`;
|
1477
|
-
const b = `${bIndicator} ${bAnnotation}${bRest}`;
|
1478
|
-
return `${aColor(a)}
|
1479
|
-
${bColor(b)}
|
1480
|
-
|
1481
|
-
`;
|
1466
|
+
function printAnnotation({ aAnnotation, aColor, aIndicator, bAnnotation, bColor, bIndicator, includeChangeCounts, omitAnnotationLines }, changeCounts) {
|
1467
|
+
if (omitAnnotationLines) {
|
1468
|
+
return "";
|
1469
|
+
}
|
1470
|
+
let aRest = "";
|
1471
|
+
let bRest = "";
|
1472
|
+
if (includeChangeCounts) {
|
1473
|
+
const aCount = String(changeCounts.a);
|
1474
|
+
const bCount = String(changeCounts.b);
|
1475
|
+
const baAnnotationLengthDiff = bAnnotation.length - aAnnotation.length;
|
1476
|
+
const aAnnotationPadding = " ".repeat(Math.max(0, baAnnotationLengthDiff));
|
1477
|
+
const bAnnotationPadding = " ".repeat(Math.max(0, -baAnnotationLengthDiff));
|
1478
|
+
const baCountLengthDiff = bCount.length - aCount.length;
|
1479
|
+
const aCountPadding = " ".repeat(Math.max(0, baCountLengthDiff));
|
1480
|
+
const bCountPadding = " ".repeat(Math.max(0, -baCountLengthDiff));
|
1481
|
+
aRest = `${aAnnotationPadding} ${aIndicator} ${aCountPadding}${aCount}`;
|
1482
|
+
bRest = `${bAnnotationPadding} ${bIndicator} ${bCountPadding}${bCount}`;
|
1483
|
+
}
|
1484
|
+
const a = `${aIndicator} ${aAnnotation}${aRest}`;
|
1485
|
+
const b = `${bIndicator} ${bAnnotation}${bRest}`;
|
1486
|
+
return `${aColor(a)}\n${bColor(b)}\n\n`;
|
1482
1487
|
}
|
1483
1488
|
function printDiffLines(diffs, truncated, options) {
|
1484
|
-
|
1485
|
-
${options.truncateAnnotation}`) : "");
|
1489
|
+
return printAnnotation(options, countChanges(diffs)) + (options.expand ? joinAlignedDiffsExpand(diffs, options) : joinAlignedDiffsNoExpand(diffs, options)) + (truncated ? options.truncateAnnotationColor(`\n${options.truncateAnnotation}`) : "");
|
1486
1490
|
}
|
1487
1491
|
function diffLinesUnified(aLines, bLines, options) {
|
1488
|
-
|
1489
|
-
|
1490
|
-
|
1491
|
-
isEmptyString(bLines) ? [] : bLines,
|
1492
|
-
normalizedOptions
|
1493
|
-
);
|
1494
|
-
return printDiffLines(diffs, truncated, normalizedOptions);
|
1492
|
+
const normalizedOptions = normalizeDiffOptions(options);
|
1493
|
+
const [diffs, truncated] = diffLinesRaw(isEmptyString(aLines) ? [] : aLines, isEmptyString(bLines) ? [] : bLines, normalizedOptions);
|
1494
|
+
return printDiffLines(diffs, truncated, normalizedOptions);
|
1495
1495
|
}
|
1496
1496
|
function diffLinesUnified2(aLinesDisplay, bLinesDisplay, aLinesCompare, bLinesCompare, options) {
|
1497
|
-
|
1498
|
-
|
1499
|
-
|
1500
|
-
|
1501
|
-
|
1502
|
-
|
1503
|
-
|
1504
|
-
|
1505
|
-
|
1506
|
-
|
1507
|
-
|
1508
|
-
|
1509
|
-
|
1510
|
-
|
1511
|
-
|
1512
|
-
|
1513
|
-
|
1514
|
-
|
1515
|
-
|
1516
|
-
|
1517
|
-
|
1518
|
-
|
1519
|
-
|
1520
|
-
|
1521
|
-
|
1522
|
-
|
1523
|
-
|
1524
|
-
|
1525
|
-
|
1526
|
-
|
1527
|
-
|
1528
|
-
bIndex += 1;
|
1529
|
-
}
|
1530
|
-
});
|
1531
|
-
return printDiffLines(diffs, truncated, normalizeDiffOptions(options));
|
1497
|
+
if (isEmptyString(aLinesDisplay) && isEmptyString(aLinesCompare)) {
|
1498
|
+
aLinesDisplay = [];
|
1499
|
+
aLinesCompare = [];
|
1500
|
+
}
|
1501
|
+
if (isEmptyString(bLinesDisplay) && isEmptyString(bLinesCompare)) {
|
1502
|
+
bLinesDisplay = [];
|
1503
|
+
bLinesCompare = [];
|
1504
|
+
}
|
1505
|
+
if (aLinesDisplay.length !== aLinesCompare.length || bLinesDisplay.length !== bLinesCompare.length) {
|
1506
|
+
return diffLinesUnified(aLinesDisplay, bLinesDisplay, options);
|
1507
|
+
}
|
1508
|
+
const [diffs, truncated] = diffLinesRaw(aLinesCompare, bLinesCompare, options);
|
1509
|
+
let aIndex = 0;
|
1510
|
+
let bIndex = 0;
|
1511
|
+
diffs.forEach((diff) => {
|
1512
|
+
switch (diff[0]) {
|
1513
|
+
case DIFF_DELETE:
|
1514
|
+
diff[1] = aLinesDisplay[aIndex];
|
1515
|
+
aIndex += 1;
|
1516
|
+
break;
|
1517
|
+
case DIFF_INSERT:
|
1518
|
+
diff[1] = bLinesDisplay[bIndex];
|
1519
|
+
bIndex += 1;
|
1520
|
+
break;
|
1521
|
+
default:
|
1522
|
+
diff[1] = bLinesDisplay[bIndex];
|
1523
|
+
aIndex += 1;
|
1524
|
+
bIndex += 1;
|
1525
|
+
}
|
1526
|
+
});
|
1527
|
+
return printDiffLines(diffs, truncated, normalizeDiffOptions(options));
|
1532
1528
|
}
|
1533
1529
|
function diffLinesRaw(aLines, bLines, options) {
|
1534
|
-
|
1535
|
-
|
1536
|
-
|
1537
|
-
|
1538
|
-
|
1539
|
-
|
1540
|
-
|
1541
|
-
|
1542
|
-
|
1543
|
-
|
1544
|
-
|
1545
|
-
|
1546
|
-
|
1547
|
-
|
1548
|
-
|
1549
|
-
|
1550
|
-
|
1551
|
-
|
1552
|
-
|
1553
|
-
|
1554
|
-
|
1555
|
-
|
1556
|
-
|
1557
|
-
|
1558
|
-
|
1559
|
-
|
1560
|
-
|
1561
|
-
|
1562
|
-
diffs.push(new Diff(DIFF_INSERT, bLines[bIndex]));
|
1563
|
-
}
|
1564
|
-
return [diffs, truncated];
|
1530
|
+
const truncate = (options === null || options === void 0 ? void 0 : options.truncateThreshold) ?? false;
|
1531
|
+
const truncateThreshold = Math.max(Math.floor((options === null || options === void 0 ? void 0 : options.truncateThreshold) ?? 0), 0);
|
1532
|
+
const aLength = truncate ? Math.min(aLines.length, truncateThreshold) : aLines.length;
|
1533
|
+
const bLength = truncate ? Math.min(bLines.length, truncateThreshold) : bLines.length;
|
1534
|
+
const truncated = aLength !== aLines.length || bLength !== bLines.length;
|
1535
|
+
const isCommon = (aIndex, bIndex) => aLines[aIndex] === bLines[bIndex];
|
1536
|
+
const diffs = [];
|
1537
|
+
let aIndex = 0;
|
1538
|
+
let bIndex = 0;
|
1539
|
+
const foundSubsequence = (nCommon, aCommon, bCommon) => {
|
1540
|
+
for (; aIndex !== aCommon; aIndex += 1) {
|
1541
|
+
diffs.push(new Diff(DIFF_DELETE, aLines[aIndex]));
|
1542
|
+
}
|
1543
|
+
for (; bIndex !== bCommon; bIndex += 1) {
|
1544
|
+
diffs.push(new Diff(DIFF_INSERT, bLines[bIndex]));
|
1545
|
+
}
|
1546
|
+
for (; nCommon !== 0; nCommon -= 1, aIndex += 1, bIndex += 1) {
|
1547
|
+
diffs.push(new Diff(DIFF_EQUAL, bLines[bIndex]));
|
1548
|
+
}
|
1549
|
+
};
|
1550
|
+
diffSequences(aLength, bLength, isCommon, foundSubsequence);
|
1551
|
+
for (; aIndex !== aLength; aIndex += 1) {
|
1552
|
+
diffs.push(new Diff(DIFF_DELETE, aLines[aIndex]));
|
1553
|
+
}
|
1554
|
+
for (; bIndex !== bLength; bIndex += 1) {
|
1555
|
+
diffs.push(new Diff(DIFF_INSERT, bLines[bIndex]));
|
1556
|
+
}
|
1557
|
+
return [diffs, truncated];
|
1565
1558
|
}
|
1566
1559
|
|
1567
1560
|
function getType(value) {
|
1568
|
-
|
1569
|
-
|
1570
|
-
|
1571
|
-
|
1572
|
-
|
1573
|
-
|
1574
|
-
|
1575
|
-
|
1576
|
-
|
1577
|
-
|
1578
|
-
|
1579
|
-
|
1580
|
-
|
1581
|
-
|
1582
|
-
|
1583
|
-
|
1584
|
-
|
1585
|
-
|
1586
|
-
|
1587
|
-
|
1588
|
-
|
1589
|
-
|
1590
|
-
|
1591
|
-
|
1592
|
-
|
1593
|
-
|
1594
|
-
|
1595
|
-
|
1596
|
-
|
1597
|
-
|
1598
|
-
|
1599
|
-
|
1600
|
-
|
1561
|
+
if (value === undefined) {
|
1562
|
+
return "undefined";
|
1563
|
+
} else if (value === null) {
|
1564
|
+
return "null";
|
1565
|
+
} else if (Array.isArray(value)) {
|
1566
|
+
return "array";
|
1567
|
+
} else if (typeof value === "boolean") {
|
1568
|
+
return "boolean";
|
1569
|
+
} else if (typeof value === "function") {
|
1570
|
+
return "function";
|
1571
|
+
} else if (typeof value === "number") {
|
1572
|
+
return "number";
|
1573
|
+
} else if (typeof value === "string") {
|
1574
|
+
return "string";
|
1575
|
+
} else if (typeof value === "bigint") {
|
1576
|
+
return "bigint";
|
1577
|
+
} else if (typeof value === "object") {
|
1578
|
+
if (value != null) {
|
1579
|
+
if (value.constructor === RegExp) {
|
1580
|
+
return "regexp";
|
1581
|
+
} else if (value.constructor === Map) {
|
1582
|
+
return "map";
|
1583
|
+
} else if (value.constructor === Set) {
|
1584
|
+
return "set";
|
1585
|
+
} else if (value.constructor === Date) {
|
1586
|
+
return "date";
|
1587
|
+
}
|
1588
|
+
}
|
1589
|
+
return "object";
|
1590
|
+
} else if (typeof value === "symbol") {
|
1591
|
+
return "symbol";
|
1592
|
+
}
|
1593
|
+
throw new Error(`value of unknown type: ${value}`);
|
1601
1594
|
}
|
1602
1595
|
|
1603
1596
|
function getNewLineSymbol(string) {
|
1604
|
-
|
1597
|
+
return string.includes("\r\n") ? "\r\n" : "\n";
|
1605
1598
|
}
|
1606
1599
|
function diffStrings(a, b, options) {
|
1607
|
-
|
1608
|
-
|
1609
|
-
|
1610
|
-
|
1611
|
-
|
1612
|
-
|
1613
|
-
|
1614
|
-
|
1615
|
-
|
1616
|
-
|
1617
|
-
|
1618
|
-
|
1619
|
-
|
1620
|
-
|
1621
|
-
|
1622
|
-
|
1623
|
-
|
1624
|
-
|
1625
|
-
|
1626
|
-
|
1627
|
-
|
1628
|
-
|
1629
|
-
|
1630
|
-
|
1631
|
-
|
1632
|
-
|
1633
|
-
|
1634
|
-
|
1635
|
-
|
1636
|
-
|
1637
|
-
|
1638
|
-
|
1639
|
-
|
1640
|
-
|
1641
|
-
|
1642
|
-
|
1643
|
-
|
1644
|
-
|
1645
|
-
}
|
1646
|
-
if (bIndex !== bLength) {
|
1647
|
-
diffs.push(new Diff(DIFF_INSERT, b.slice(bIndex)));
|
1648
|
-
}
|
1649
|
-
return [diffs, truncated];
|
1600
|
+
const truncate = (options === null || options === void 0 ? void 0 : options.truncateThreshold) ?? false;
|
1601
|
+
const truncateThreshold = Math.max(Math.floor((options === null || options === void 0 ? void 0 : options.truncateThreshold) ?? 0), 0);
|
1602
|
+
let aLength = a.length;
|
1603
|
+
let bLength = b.length;
|
1604
|
+
if (truncate) {
|
1605
|
+
const aMultipleLines = a.includes("\n");
|
1606
|
+
const bMultipleLines = b.includes("\n");
|
1607
|
+
const aNewLineSymbol = getNewLineSymbol(a);
|
1608
|
+
const bNewLineSymbol = getNewLineSymbol(b);
|
1609
|
+
const _a = aMultipleLines ? `${a.split(aNewLineSymbol, truncateThreshold).join(aNewLineSymbol)}\n` : a;
|
1610
|
+
const _b = bMultipleLines ? `${b.split(bNewLineSymbol, truncateThreshold).join(bNewLineSymbol)}\n` : b;
|
1611
|
+
aLength = _a.length;
|
1612
|
+
bLength = _b.length;
|
1613
|
+
}
|
1614
|
+
const truncated = aLength !== a.length || bLength !== b.length;
|
1615
|
+
const isCommon = (aIndex, bIndex) => a[aIndex] === b[bIndex];
|
1616
|
+
let aIndex = 0;
|
1617
|
+
let bIndex = 0;
|
1618
|
+
const diffs = [];
|
1619
|
+
const foundSubsequence = (nCommon, aCommon, bCommon) => {
|
1620
|
+
if (aIndex !== aCommon) {
|
1621
|
+
diffs.push(new Diff(DIFF_DELETE, a.slice(aIndex, aCommon)));
|
1622
|
+
}
|
1623
|
+
if (bIndex !== bCommon) {
|
1624
|
+
diffs.push(new Diff(DIFF_INSERT, b.slice(bIndex, bCommon)));
|
1625
|
+
}
|
1626
|
+
aIndex = aCommon + nCommon;
|
1627
|
+
bIndex = bCommon + nCommon;
|
1628
|
+
diffs.push(new Diff(DIFF_EQUAL, b.slice(bCommon, bIndex)));
|
1629
|
+
};
|
1630
|
+
diffSequences(aLength, bLength, isCommon, foundSubsequence);
|
1631
|
+
if (aIndex !== aLength) {
|
1632
|
+
diffs.push(new Diff(DIFF_DELETE, a.slice(aIndex)));
|
1633
|
+
}
|
1634
|
+
if (bIndex !== bLength) {
|
1635
|
+
diffs.push(new Diff(DIFF_INSERT, b.slice(bIndex)));
|
1636
|
+
}
|
1637
|
+
return [diffs, truncated];
|
1650
1638
|
}
|
1651
1639
|
|
1652
1640
|
function concatenateRelevantDiffs(op, diffs, changeColor) {
|
1653
|
-
|
1654
|
-
(reduced, diff) => reduced + (diff[0] === DIFF_EQUAL ? diff[1] : diff[0] === op && diff[1].length !== 0 ? changeColor(diff[1]) : ""),
|
1655
|
-
""
|
1656
|
-
);
|
1641
|
+
return diffs.reduce((reduced, diff) => reduced + (diff[0] === DIFF_EQUAL ? diff[1] : diff[0] === op && diff[1].length !== 0 ? changeColor(diff[1]) : ""), "");
|
1657
1642
|
}
|
1658
1643
|
class ChangeBuffer {
|
1659
|
-
|
1660
|
-
|
1661
|
-
|
1662
|
-
|
1663
|
-
|
1664
|
-
|
1665
|
-
|
1666
|
-
|
1667
|
-
|
1668
|
-
|
1669
|
-
|
1670
|
-
|
1671
|
-
|
1672
|
-
|
1673
|
-
|
1674
|
-
|
1675
|
-
|
1676
|
-
|
1677
|
-
|
1678
|
-
|
1679
|
-
|
1680
|
-
|
1681
|
-
|
1682
|
-
|
1683
|
-
|
1684
|
-
|
1685
|
-
|
1686
|
-
|
1687
|
-
|
1688
|
-
|
1689
|
-
|
1690
|
-
|
1691
|
-
|
1692
|
-
|
1693
|
-
|
1694
|
-
|
1695
|
-
|
1696
|
-
|
1697
|
-
|
1698
|
-
|
1699
|
-
|
1700
|
-
|
1701
|
-
|
1702
|
-
|
1703
|
-
|
1704
|
-
|
1705
|
-
|
1706
|
-
this.pushDiff(diff);
|
1707
|
-
}
|
1708
|
-
}
|
1709
|
-
// Output from buffer.
|
1710
|
-
moveLinesTo(lines) {
|
1711
|
-
if (!this.isLineEmpty()) {
|
1712
|
-
this.pushLine();
|
1713
|
-
}
|
1714
|
-
lines.push(...this.lines);
|
1715
|
-
this.lines.length = 0;
|
1716
|
-
}
|
1644
|
+
op;
|
1645
|
+
line;
|
1646
|
+
lines;
|
1647
|
+
changeColor;
|
1648
|
+
constructor(op, changeColor) {
|
1649
|
+
this.op = op;
|
1650
|
+
this.line = [];
|
1651
|
+
this.lines = [];
|
1652
|
+
this.changeColor = changeColor;
|
1653
|
+
}
|
1654
|
+
pushSubstring(substring) {
|
1655
|
+
this.pushDiff(new Diff(this.op, substring));
|
1656
|
+
}
|
1657
|
+
pushLine() {
|
1658
|
+
this.lines.push(this.line.length !== 1 ? new Diff(this.op, concatenateRelevantDiffs(this.op, this.line, this.changeColor)) : this.line[0][0] === this.op ? this.line[0] : new Diff(this.op, this.line[0][1]));
|
1659
|
+
this.line.length = 0;
|
1660
|
+
}
|
1661
|
+
isLineEmpty() {
|
1662
|
+
return this.line.length === 0;
|
1663
|
+
}
|
1664
|
+
pushDiff(diff) {
|
1665
|
+
this.line.push(diff);
|
1666
|
+
}
|
1667
|
+
align(diff) {
|
1668
|
+
const string = diff[1];
|
1669
|
+
if (string.includes("\n")) {
|
1670
|
+
const substrings = string.split("\n");
|
1671
|
+
const iLast = substrings.length - 1;
|
1672
|
+
substrings.forEach((substring, i) => {
|
1673
|
+
if (i < iLast) {
|
1674
|
+
this.pushSubstring(substring);
|
1675
|
+
this.pushLine();
|
1676
|
+
} else if (substring.length !== 0) {
|
1677
|
+
this.pushSubstring(substring);
|
1678
|
+
}
|
1679
|
+
});
|
1680
|
+
} else {
|
1681
|
+
this.pushDiff(diff);
|
1682
|
+
}
|
1683
|
+
}
|
1684
|
+
moveLinesTo(lines) {
|
1685
|
+
if (!this.isLineEmpty()) {
|
1686
|
+
this.pushLine();
|
1687
|
+
}
|
1688
|
+
lines.push(...this.lines);
|
1689
|
+
this.lines.length = 0;
|
1690
|
+
}
|
1717
1691
|
}
|
1718
1692
|
class CommonBuffer {
|
1719
|
-
|
1720
|
-
|
1721
|
-
|
1722
|
-
|
1723
|
-
|
1724
|
-
|
1725
|
-
|
1726
|
-
|
1727
|
-
|
1728
|
-
|
1729
|
-
|
1730
|
-
|
1731
|
-
|
1732
|
-
|
1733
|
-
|
1734
|
-
|
1735
|
-
|
1736
|
-
|
1737
|
-
|
1738
|
-
|
1739
|
-
|
1740
|
-
|
1741
|
-
|
1742
|
-
|
1743
|
-
|
1744
|
-
|
1745
|
-
|
1746
|
-
|
1747
|
-
|
1748
|
-
|
1749
|
-
|
1750
|
-
|
1751
|
-
|
1752
|
-
|
1753
|
-
|
1754
|
-
|
1755
|
-
|
1756
|
-
|
1757
|
-
|
1758
|
-
|
1759
|
-
|
1760
|
-
|
1761
|
-
|
1762
|
-
|
1763
|
-
|
1764
|
-
|
1765
|
-
|
1766
|
-
|
1767
|
-
|
1768
|
-
|
1769
|
-
|
1770
|
-
|
1771
|
-
|
1772
|
-
|
1773
|
-
return this.lines;
|
1774
|
-
}
|
1693
|
+
deleteBuffer;
|
1694
|
+
insertBuffer;
|
1695
|
+
lines;
|
1696
|
+
constructor(deleteBuffer, insertBuffer) {
|
1697
|
+
this.deleteBuffer = deleteBuffer;
|
1698
|
+
this.insertBuffer = insertBuffer;
|
1699
|
+
this.lines = [];
|
1700
|
+
}
|
1701
|
+
pushDiffCommonLine(diff) {
|
1702
|
+
this.lines.push(diff);
|
1703
|
+
}
|
1704
|
+
pushDiffChangeLines(diff) {
|
1705
|
+
const isDiffEmpty = diff[1].length === 0;
|
1706
|
+
if (!isDiffEmpty || this.deleteBuffer.isLineEmpty()) {
|
1707
|
+
this.deleteBuffer.pushDiff(diff);
|
1708
|
+
}
|
1709
|
+
if (!isDiffEmpty || this.insertBuffer.isLineEmpty()) {
|
1710
|
+
this.insertBuffer.pushDiff(diff);
|
1711
|
+
}
|
1712
|
+
}
|
1713
|
+
flushChangeLines() {
|
1714
|
+
this.deleteBuffer.moveLinesTo(this.lines);
|
1715
|
+
this.insertBuffer.moveLinesTo(this.lines);
|
1716
|
+
}
|
1717
|
+
align(diff) {
|
1718
|
+
const op = diff[0];
|
1719
|
+
const string = diff[1];
|
1720
|
+
if (string.includes("\n")) {
|
1721
|
+
const substrings = string.split("\n");
|
1722
|
+
const iLast = substrings.length - 1;
|
1723
|
+
substrings.forEach((substring, i) => {
|
1724
|
+
if (i === 0) {
|
1725
|
+
const subdiff = new Diff(op, substring);
|
1726
|
+
if (this.deleteBuffer.isLineEmpty() && this.insertBuffer.isLineEmpty()) {
|
1727
|
+
this.flushChangeLines();
|
1728
|
+
this.pushDiffCommonLine(subdiff);
|
1729
|
+
} else {
|
1730
|
+
this.pushDiffChangeLines(subdiff);
|
1731
|
+
this.flushChangeLines();
|
1732
|
+
}
|
1733
|
+
} else if (i < iLast) {
|
1734
|
+
this.pushDiffCommonLine(new Diff(op, substring));
|
1735
|
+
} else if (substring.length !== 0) {
|
1736
|
+
this.pushDiffChangeLines(new Diff(op, substring));
|
1737
|
+
}
|
1738
|
+
});
|
1739
|
+
} else {
|
1740
|
+
this.pushDiffChangeLines(diff);
|
1741
|
+
}
|
1742
|
+
}
|
1743
|
+
getLines() {
|
1744
|
+
this.flushChangeLines();
|
1745
|
+
return this.lines;
|
1746
|
+
}
|
1775
1747
|
}
|
1776
1748
|
function getAlignedDiffs(diffs, changeColor) {
|
1777
|
-
|
1778
|
-
|
1779
|
-
|
1780
|
-
|
1781
|
-
|
1782
|
-
|
1783
|
-
|
1784
|
-
|
1785
|
-
|
1786
|
-
|
1787
|
-
|
1788
|
-
|
1789
|
-
|
1790
|
-
|
1791
|
-
|
1792
|
-
return commonBuffer.getLines();
|
1749
|
+
const deleteBuffer = new ChangeBuffer(DIFF_DELETE, changeColor);
|
1750
|
+
const insertBuffer = new ChangeBuffer(DIFF_INSERT, changeColor);
|
1751
|
+
const commonBuffer = new CommonBuffer(deleteBuffer, insertBuffer);
|
1752
|
+
diffs.forEach((diff) => {
|
1753
|
+
switch (diff[0]) {
|
1754
|
+
case DIFF_DELETE:
|
1755
|
+
deleteBuffer.align(diff);
|
1756
|
+
break;
|
1757
|
+
case DIFF_INSERT:
|
1758
|
+
insertBuffer.align(diff);
|
1759
|
+
break;
|
1760
|
+
default: commonBuffer.align(diff);
|
1761
|
+
}
|
1762
|
+
});
|
1763
|
+
return commonBuffer.getLines();
|
1793
1764
|
}
|
1794
1765
|
|
1795
1766
|
function hasCommonDiff(diffs, isMultiline) {
|
1796
|
-
|
1797
|
-
|
1798
|
-
|
1799
|
-
|
1800
|
-
|
1801
|
-
}
|
1802
|
-
return diffs.some((diff) => diff[0] === DIFF_EQUAL);
|
1767
|
+
if (isMultiline) {
|
1768
|
+
const iLast = diffs.length - 1;
|
1769
|
+
return diffs.some((diff, i) => diff[0] === DIFF_EQUAL && (i !== iLast || diff[1] !== "\n"));
|
1770
|
+
}
|
1771
|
+
return diffs.some((diff) => diff[0] === DIFF_EQUAL);
|
1803
1772
|
}
|
1804
1773
|
function diffStringsUnified(a, b, options) {
|
1805
|
-
|
1806
|
-
|
1807
|
-
|
1808
|
-
|
1809
|
-
|
1810
|
-
|
1811
|
-
|
1812
|
-
|
1813
|
-
|
1814
|
-
|
1815
|
-
);
|
1816
|
-
if (hasCommonDiff(diffs, isMultiline)) {
|
1817
|
-
const optionsNormalized = normalizeDiffOptions(options);
|
1818
|
-
const lines = getAlignedDiffs(diffs, optionsNormalized.changeColor);
|
1819
|
-
return printDiffLines(lines, truncated, optionsNormalized);
|
1820
|
-
}
|
1821
|
-
}
|
1822
|
-
return diffLinesUnified(a.split("\n"), b.split("\n"), options);
|
1774
|
+
if (a !== b && a.length !== 0 && b.length !== 0) {
|
1775
|
+
const isMultiline = a.includes("\n") || b.includes("\n");
|
1776
|
+
const [diffs, truncated] = diffStringsRaw(isMultiline ? `${a}\n` : a, isMultiline ? `${b}\n` : b, true, options);
|
1777
|
+
if (hasCommonDiff(diffs, isMultiline)) {
|
1778
|
+
const optionsNormalized = normalizeDiffOptions(options);
|
1779
|
+
const lines = getAlignedDiffs(diffs, optionsNormalized.changeColor);
|
1780
|
+
return printDiffLines(lines, truncated, optionsNormalized);
|
1781
|
+
}
|
1782
|
+
}
|
1783
|
+
return diffLinesUnified(a.split("\n"), b.split("\n"), options);
|
1823
1784
|
}
|
1824
1785
|
function diffStringsRaw(a, b, cleanup, options) {
|
1825
|
-
|
1826
|
-
|
1827
|
-
|
1828
|
-
|
1829
|
-
|
1786
|
+
const [diffs, truncated] = diffStrings(a, b, options);
|
1787
|
+
if (cleanup) {
|
1788
|
+
diff_cleanupSemantic(diffs);
|
1789
|
+
}
|
1790
|
+
return [diffs, truncated];
|
1830
1791
|
}
|
1831
1792
|
|
1832
1793
|
function getCommonMessage(message, options) {
|
1833
|
-
|
1834
|
-
|
1794
|
+
const { commonColor } = normalizeDiffOptions(options);
|
1795
|
+
return commonColor(message);
|
1835
1796
|
}
|
1836
|
-
const {
|
1837
|
-
AsymmetricMatcher,
|
1838
|
-
DOMCollection,
|
1839
|
-
DOMElement,
|
1840
|
-
Immutable,
|
1841
|
-
ReactElement,
|
1842
|
-
ReactTestComponent
|
1843
|
-
} = plugins;
|
1797
|
+
const { AsymmetricMatcher, DOMCollection, DOMElement, Immutable, ReactElement, ReactTestComponent } = plugins;
|
1844
1798
|
const PLUGINS = [
|
1845
|
-
|
1846
|
-
|
1847
|
-
|
1848
|
-
|
1849
|
-
|
1850
|
-
|
1851
|
-
|
1799
|
+
ReactTestComponent,
|
1800
|
+
ReactElement,
|
1801
|
+
DOMElement,
|
1802
|
+
DOMCollection,
|
1803
|
+
Immutable,
|
1804
|
+
AsymmetricMatcher,
|
1805
|
+
plugins.Error
|
1852
1806
|
];
|
1853
1807
|
const FORMAT_OPTIONS = {
|
1854
|
-
|
1855
|
-
|
1808
|
+
maxDepth: 20,
|
1809
|
+
plugins: PLUGINS
|
1856
1810
|
};
|
1857
1811
|
const FALLBACK_FORMAT_OPTIONS = {
|
1858
|
-
|
1859
|
-
|
1860
|
-
|
1812
|
+
callToJSON: false,
|
1813
|
+
maxDepth: 8,
|
1814
|
+
plugins: PLUGINS
|
1861
1815
|
};
|
1816
|
+
/**
|
1817
|
+
* @param a Expected value
|
1818
|
+
* @param b Received value
|
1819
|
+
* @param options Diff options
|
1820
|
+
* @returns {string | null} a string diff
|
1821
|
+
*/
|
1862
1822
|
function diff(a, b, options) {
|
1863
|
-
|
1864
|
-
|
1865
|
-
|
1866
|
-
|
1867
|
-
|
1868
|
-
|
1869
|
-
|
1870
|
-
|
1871
|
-
|
1872
|
-
|
1873
|
-
|
1874
|
-
|
1875
|
-
|
1876
|
-
|
1877
|
-
|
1878
|
-
|
1879
|
-
|
1880
|
-
|
1881
|
-
|
1882
|
-
|
1883
|
-
|
1884
|
-
|
1885
|
-
|
1886
|
-
|
1887
|
-
|
1888
|
-
|
1889
|
-
|
1890
|
-
|
1891
|
-
${
|
1892
|
-
|
1893
|
-
|
1894
|
-
|
1895
|
-
|
1896
|
-
|
1897
|
-
|
1898
|
-
|
1899
|
-
|
1900
|
-
|
1901
|
-
|
1902
|
-
|
1903
|
-
|
1904
|
-
|
1905
|
-
case "number":
|
1906
|
-
return comparePrimitive(a, b, options);
|
1907
|
-
case "map":
|
1908
|
-
return compareObjects(sortMap(a), sortMap(b), options);
|
1909
|
-
case "set":
|
1910
|
-
return compareObjects(sortSet(a), sortSet(b), options);
|
1911
|
-
default:
|
1912
|
-
return compareObjects(a, b, options);
|
1913
|
-
}
|
1823
|
+
if (Object.is(a, b)) {
|
1824
|
+
return "";
|
1825
|
+
}
|
1826
|
+
const aType = getType(a);
|
1827
|
+
let expectedType = aType;
|
1828
|
+
let omitDifference = false;
|
1829
|
+
if (aType === "object" && typeof a.asymmetricMatch === "function") {
|
1830
|
+
if (a.$$typeof !== Symbol.for("jest.asymmetricMatcher")) {
|
1831
|
+
return undefined;
|
1832
|
+
}
|
1833
|
+
if (typeof a.getExpectedType !== "function") {
|
1834
|
+
return undefined;
|
1835
|
+
}
|
1836
|
+
expectedType = a.getExpectedType();
|
1837
|
+
omitDifference = expectedType === "string";
|
1838
|
+
}
|
1839
|
+
if (expectedType !== getType(b)) {
|
1840
|
+
const { aAnnotation, aColor, aIndicator, bAnnotation, bColor, bIndicator } = normalizeDiffOptions(options);
|
1841
|
+
const formatOptions = getFormatOptions(FALLBACK_FORMAT_OPTIONS, options);
|
1842
|
+
let aDisplay = format(a, formatOptions);
|
1843
|
+
let bDisplay = format(b, formatOptions);
|
1844
|
+
const MAX_LENGTH = 1e5;
|
1845
|
+
function truncate(s) {
|
1846
|
+
return s.length <= MAX_LENGTH ? s : `${s.slice(0, MAX_LENGTH)}...`;
|
1847
|
+
}
|
1848
|
+
aDisplay = truncate(aDisplay);
|
1849
|
+
bDisplay = truncate(bDisplay);
|
1850
|
+
const aDiff = `${aColor(`${aIndicator} ${aAnnotation}:`)} \n${aDisplay}`;
|
1851
|
+
const bDiff = `${bColor(`${bIndicator} ${bAnnotation}:`)} \n${bDisplay}`;
|
1852
|
+
return `${aDiff}\n\n${bDiff}`;
|
1853
|
+
}
|
1854
|
+
if (omitDifference) {
|
1855
|
+
return undefined;
|
1856
|
+
}
|
1857
|
+
switch (aType) {
|
1858
|
+
case "string": return diffLinesUnified(a.split("\n"), b.split("\n"), options);
|
1859
|
+
case "boolean":
|
1860
|
+
case "number": return comparePrimitive(a, b, options);
|
1861
|
+
case "map": return compareObjects(sortMap(a), sortMap(b), options);
|
1862
|
+
case "set": return compareObjects(sortSet(a), sortSet(b), options);
|
1863
|
+
default: return compareObjects(a, b, options);
|
1864
|
+
}
|
1914
1865
|
}
|
1915
1866
|
function comparePrimitive(a, b, options) {
|
1916
|
-
|
1917
|
-
|
1918
|
-
|
1867
|
+
const aFormat = format(a, FORMAT_OPTIONS);
|
1868
|
+
const bFormat = format(b, FORMAT_OPTIONS);
|
1869
|
+
return aFormat === bFormat ? "" : diffLinesUnified(aFormat.split("\n"), bFormat.split("\n"), options);
|
1919
1870
|
}
|
1920
1871
|
function sortMap(map) {
|
1921
|
-
|
1872
|
+
return new Map(Array.from(map.entries()).sort());
|
1922
1873
|
}
|
1923
1874
|
function sortSet(set) {
|
1924
|
-
|
1875
|
+
return new Set(Array.from(set.values()).sort());
|
1925
1876
|
}
|
1926
1877
|
function compareObjects(a, b, options) {
|
1927
|
-
|
1928
|
-
|
1929
|
-
|
1930
|
-
|
1931
|
-
|
1932
|
-
|
1933
|
-
|
1934
|
-
|
1935
|
-
|
1936
|
-
|
1937
|
-
|
1938
|
-
|
1939
|
-
|
1940
|
-
|
1941
|
-
|
1942
|
-
|
1943
|
-
|
1944
|
-
|
1945
|
-
${difference}`;
|
1946
|
-
}
|
1947
|
-
}
|
1948
|
-
return difference;
|
1878
|
+
let difference;
|
1879
|
+
let hasThrown = false;
|
1880
|
+
try {
|
1881
|
+
const formatOptions = getFormatOptions(FORMAT_OPTIONS, options);
|
1882
|
+
difference = getObjectsDifference(a, b, formatOptions, options);
|
1883
|
+
} catch {
|
1884
|
+
hasThrown = true;
|
1885
|
+
}
|
1886
|
+
const noDiffMessage = getCommonMessage(NO_DIFF_MESSAGE, options);
|
1887
|
+
if (difference === undefined || difference === noDiffMessage) {
|
1888
|
+
const formatOptions = getFormatOptions(FALLBACK_FORMAT_OPTIONS, options);
|
1889
|
+
difference = getObjectsDifference(a, b, formatOptions, options);
|
1890
|
+
if (difference !== noDiffMessage && !hasThrown) {
|
1891
|
+
difference = `${getCommonMessage(SIMILAR_MESSAGE, options)}\n\n${difference}`;
|
1892
|
+
}
|
1893
|
+
}
|
1894
|
+
return difference;
|
1949
1895
|
}
|
1950
1896
|
function getFormatOptions(formatOptions, options) {
|
1951
|
-
|
1952
|
-
|
1953
|
-
|
1954
|
-
|
1955
|
-
|
1956
|
-
|
1957
|
-
|
1897
|
+
const { compareKeys, printBasicPrototype, maxDepth } = normalizeDiffOptions(options);
|
1898
|
+
return {
|
1899
|
+
...formatOptions,
|
1900
|
+
compareKeys,
|
1901
|
+
printBasicPrototype,
|
1902
|
+
maxDepth: maxDepth ?? formatOptions.maxDepth
|
1903
|
+
};
|
1958
1904
|
}
|
1959
1905
|
function getObjectsDifference(a, b, formatOptions, options) {
|
1960
|
-
|
1961
|
-
|
1962
|
-
|
1963
|
-
|
1964
|
-
|
1965
|
-
|
1966
|
-
|
1967
|
-
|
1968
|
-
|
1969
|
-
|
1970
|
-
|
1971
|
-
|
1972
|
-
|
1973
|
-
options
|
1974
|
-
);
|
1975
|
-
}
|
1906
|
+
const formatOptionsZeroIndent = {
|
1907
|
+
...formatOptions,
|
1908
|
+
indent: 0
|
1909
|
+
};
|
1910
|
+
const aCompare = format(a, formatOptionsZeroIndent);
|
1911
|
+
const bCompare = format(b, formatOptionsZeroIndent);
|
1912
|
+
if (aCompare === bCompare) {
|
1913
|
+
return getCommonMessage(NO_DIFF_MESSAGE, options);
|
1914
|
+
} else {
|
1915
|
+
const aDisplay = format(a, formatOptions);
|
1916
|
+
const bDisplay = format(b, formatOptions);
|
1917
|
+
return diffLinesUnified2(aDisplay.split("\n"), bDisplay.split("\n"), aCompare.split("\n"), bCompare.split("\n"), options);
|
1918
|
+
}
|
1976
1919
|
}
|
1977
1920
|
const MAX_DIFF_STRING_LENGTH = 2e4;
|
1978
1921
|
function isAsymmetricMatcher(data) {
|
1979
|
-
|
1980
|
-
|
1922
|
+
const type = getType$1(data);
|
1923
|
+
return type === "Object" && typeof data.asymmetricMatch === "function";
|
1981
1924
|
}
|
1982
1925
|
function isReplaceable(obj1, obj2) {
|
1983
|
-
|
1984
|
-
|
1985
|
-
|
1926
|
+
const obj1Type = getType$1(obj1);
|
1927
|
+
const obj2Type = getType$1(obj2);
|
1928
|
+
return obj1Type === obj2Type && (obj1Type === "Object" || obj1Type === "Array");
|
1986
1929
|
}
|
1987
1930
|
function printDiffOrStringify(received, expected, options) {
|
1988
|
-
|
1989
|
-
|
1990
|
-
|
1991
|
-
|
1992
|
-
|
1993
|
-
|
1994
|
-
|
1995
|
-
|
1996
|
-
|
1997
|
-
|
1998
|
-
|
1999
|
-
|
2000
|
-
|
2001
|
-
|
2002
|
-
|
2003
|
-
|
2004
|
-
|
2005
|
-
const clonedExpected = deepClone(expected, { forceWritable: true });
|
2006
|
-
const clonedReceived = deepClone(received, { forceWritable: true });
|
2007
|
-
const { replacedExpected, replacedActual } = replaceAsymmetricMatcher(clonedReceived, clonedExpected);
|
2008
|
-
const difference = diff(replacedExpected, replacedActual, options);
|
2009
|
-
return difference;
|
1931
|
+
const { aAnnotation, bAnnotation } = normalizeDiffOptions(options);
|
1932
|
+
if (typeof expected === "string" && typeof received === "string" && expected.length > 0 && received.length > 0 && expected.length <= MAX_DIFF_STRING_LENGTH && received.length <= MAX_DIFF_STRING_LENGTH && expected !== received) {
|
1933
|
+
if (expected.includes("\n") || received.includes("\n")) {
|
1934
|
+
return diffStringsUnified(expected, received, options);
|
1935
|
+
}
|
1936
|
+
const [diffs] = diffStringsRaw(expected, received, true);
|
1937
|
+
const hasCommonDiff = diffs.some((diff) => diff[0] === DIFF_EQUAL);
|
1938
|
+
const printLabel = getLabelPrinter(aAnnotation, bAnnotation);
|
1939
|
+
const expectedLine = printLabel(aAnnotation) + printExpected(getCommonAndChangedSubstrings(diffs, DIFF_DELETE, hasCommonDiff));
|
1940
|
+
const receivedLine = printLabel(bAnnotation) + printReceived(getCommonAndChangedSubstrings(diffs, DIFF_INSERT, hasCommonDiff));
|
1941
|
+
return `${expectedLine}\n${receivedLine}`;
|
1942
|
+
}
|
1943
|
+
const clonedExpected = deepClone(expected, { forceWritable: true });
|
1944
|
+
const clonedReceived = deepClone(received, { forceWritable: true });
|
1945
|
+
const { replacedExpected, replacedActual } = replaceAsymmetricMatcher(clonedReceived, clonedExpected);
|
1946
|
+
const difference = diff(replacedExpected, replacedActual, options);
|
1947
|
+
return difference;
|
2010
1948
|
}
|
2011
|
-
function replaceAsymmetricMatcher(actual, expected, actualReplaced =
|
2012
|
-
|
2013
|
-
|
2014
|
-
|
2015
|
-
|
2016
|
-
|
2017
|
-
|
2018
|
-
|
2019
|
-
|
2020
|
-
|
2021
|
-
|
2022
|
-
|
2023
|
-
|
2024
|
-
|
2025
|
-
|
2026
|
-
|
2027
|
-
|
2028
|
-
|
2029
|
-
|
2030
|
-
|
2031
|
-
|
2032
|
-
|
2033
|
-
|
2034
|
-
|
2035
|
-
|
2036
|
-
|
2037
|
-
|
2038
|
-
|
2039
|
-
|
2040
|
-
|
2041
|
-
|
2042
|
-
|
2043
|
-
|
2044
|
-
|
2045
|
-
|
2046
|
-
|
2047
|
-
|
2048
|
-
|
2049
|
-
|
2050
|
-
|
2051
|
-
|
2052
|
-
|
1949
|
+
function replaceAsymmetricMatcher(actual, expected, actualReplaced = new WeakSet(), expectedReplaced = new WeakSet()) {
|
1950
|
+
if (actual instanceof Error && expected instanceof Error && typeof actual.cause !== "undefined" && typeof expected.cause === "undefined") {
|
1951
|
+
delete actual.cause;
|
1952
|
+
return {
|
1953
|
+
replacedActual: actual,
|
1954
|
+
replacedExpected: expected
|
1955
|
+
};
|
1956
|
+
}
|
1957
|
+
if (!isReplaceable(actual, expected)) {
|
1958
|
+
return {
|
1959
|
+
replacedActual: actual,
|
1960
|
+
replacedExpected: expected
|
1961
|
+
};
|
1962
|
+
}
|
1963
|
+
if (actualReplaced.has(actual) || expectedReplaced.has(expected)) {
|
1964
|
+
return {
|
1965
|
+
replacedActual: actual,
|
1966
|
+
replacedExpected: expected
|
1967
|
+
};
|
1968
|
+
}
|
1969
|
+
actualReplaced.add(actual);
|
1970
|
+
expectedReplaced.add(expected);
|
1971
|
+
getOwnProperties(expected).forEach((key) => {
|
1972
|
+
const expectedValue = expected[key];
|
1973
|
+
const actualValue = actual[key];
|
1974
|
+
if (isAsymmetricMatcher(expectedValue)) {
|
1975
|
+
if (expectedValue.asymmetricMatch(actualValue)) {
|
1976
|
+
actual[key] = expectedValue;
|
1977
|
+
}
|
1978
|
+
} else if (isAsymmetricMatcher(actualValue)) {
|
1979
|
+
if (actualValue.asymmetricMatch(expectedValue)) {
|
1980
|
+
expected[key] = actualValue;
|
1981
|
+
}
|
1982
|
+
} else if (isReplaceable(actualValue, expectedValue)) {
|
1983
|
+
const replaced = replaceAsymmetricMatcher(actualValue, expectedValue, actualReplaced, expectedReplaced);
|
1984
|
+
actual[key] = replaced.replacedActual;
|
1985
|
+
expected[key] = replaced.replacedExpected;
|
1986
|
+
}
|
1987
|
+
});
|
1988
|
+
return {
|
1989
|
+
replacedActual: actual,
|
1990
|
+
replacedExpected: expected
|
1991
|
+
};
|
2053
1992
|
}
|
2054
1993
|
function getLabelPrinter(...strings) {
|
2055
|
-
|
2056
|
-
|
2057
|
-
0
|
2058
|
-
);
|
2059
|
-
return (string) => `${string}: ${" ".repeat(maxLength - string.length)}`;
|
1994
|
+
const maxLength = strings.reduce((max, string) => string.length > max ? string.length : max, 0);
|
1995
|
+
return (string) => `${string}: ${" ".repeat(maxLength - string.length)}`;
|
2060
1996
|
}
|
2061
|
-
const SPACE_SYMBOL = "
|
1997
|
+
const SPACE_SYMBOL = "·";
|
2062
1998
|
function replaceTrailingSpaces(text) {
|
2063
|
-
|
1999
|
+
return text.replace(/\s+$/gm, (spaces) => SPACE_SYMBOL.repeat(spaces.length));
|
2064
2000
|
}
|
2065
2001
|
function printReceived(object) {
|
2066
|
-
|
2002
|
+
return c.red(replaceTrailingSpaces(stringify(object)));
|
2067
2003
|
}
|
2068
2004
|
function printExpected(value) {
|
2069
|
-
|
2005
|
+
return c.green(replaceTrailingSpaces(stringify(value)));
|
2070
2006
|
}
|
2071
2007
|
function getCommonAndChangedSubstrings(diffs, op, hasCommonDiff) {
|
2072
|
-
|
2073
|
-
(reduced, diff2) => reduced + (diff2[0] === DIFF_EQUAL ? diff2[1] : diff2[0] === op ? hasCommonDiff ? c.inverse(diff2[1]) : diff2[1] : ""),
|
2074
|
-
""
|
2075
|
-
);
|
2008
|
+
return diffs.reduce((reduced, diff) => reduced + (diff[0] === DIFF_EQUAL ? diff[1] : diff[0] === op ? hasCommonDiff ? c.inverse(diff[1]) : diff[1] : ""), "");
|
2076
2009
|
}
|
2077
2010
|
|
2078
2011
|
export { DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, Diff, diff, diffLinesRaw, diffLinesUnified, diffLinesUnified2, diffStringsRaw, diffStringsUnified, getLabelPrinter, printDiffOrStringify, replaceAsymmetricMatcher };
|