@vitest/utils 0.32.0 → 0.32.2
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/diff.d.ts +108 -8
- package/dist/diff.js +1043 -102
- package/dist/error.d.ts +1 -3
- package/dist/error.js +6 -9
- package/package.json +2 -2
package/dist/diff.d.ts
CHANGED
@@ -1,14 +1,114 @@
|
|
1
|
+
import { CompareKeys } from 'pretty-format';
|
2
|
+
|
3
|
+
/**
|
4
|
+
* Diff Match and Patch
|
5
|
+
* Copyright 2018 The diff-match-patch Authors.
|
6
|
+
* https://github.com/google/diff-match-patch
|
7
|
+
*
|
8
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
9
|
+
* you may not use this file except in compliance with the License.
|
10
|
+
* You may obtain a copy of the License at
|
11
|
+
*
|
12
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
13
|
+
*
|
14
|
+
* Unless required by applicable law or agreed to in writing, software
|
15
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
16
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
17
|
+
* See the License for the specific language governing permissions and
|
18
|
+
* limitations under the License.
|
19
|
+
*/
|
20
|
+
/**
|
21
|
+
* @fileoverview Computes the difference between two texts to create a patch.
|
22
|
+
* Applies the patch onto another text, allowing for errors.
|
23
|
+
* @author fraser@google.com (Neil Fraser)
|
24
|
+
*/
|
25
|
+
/**
|
26
|
+
* CHANGES by pedrottimark to diff_match_patch_uncompressed.ts file:
|
27
|
+
*
|
28
|
+
* 1. Delete anything not needed to use diff_cleanupSemantic method
|
29
|
+
* 2. Convert from prototype properties to var declarations
|
30
|
+
* 3. Convert Diff to class from constructor and prototype
|
31
|
+
* 4. Add type annotations for arguments and return values
|
32
|
+
* 5. Add exports
|
33
|
+
*/
|
34
|
+
/**
|
35
|
+
* The data structure representing a diff is an array of tuples:
|
36
|
+
* [[DIFF_DELETE, 'Hello'], [DIFF_INSERT, 'Goodbye'], [DIFF_EQUAL, ' world.']]
|
37
|
+
* which means: delete 'Hello', add 'Goodbye' and keep ' world.'
|
38
|
+
*/
|
39
|
+
declare const DIFF_DELETE = -1;
|
40
|
+
declare const DIFF_INSERT = 1;
|
41
|
+
declare const DIFF_EQUAL = 0;
|
42
|
+
/**
|
43
|
+
* Class representing one diff tuple.
|
44
|
+
* Attempts to look like a two-element array (which is what this used to be).
|
45
|
+
* @param {number} op Operation, one of: DIFF_DELETE, DIFF_INSERT, DIFF_EQUAL.
|
46
|
+
* @param {string} text Text to be deleted, inserted, or retained.
|
47
|
+
* @constructor
|
48
|
+
*/
|
49
|
+
declare class Diff {
|
50
|
+
0: number;
|
51
|
+
1: string;
|
52
|
+
constructor(op: number, text: string);
|
53
|
+
}
|
54
|
+
|
55
|
+
/**
|
56
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
57
|
+
*
|
58
|
+
* This source code is licensed under the MIT license found in the
|
59
|
+
* LICENSE file in the root directory of this source tree.
|
60
|
+
*/
|
61
|
+
|
62
|
+
type DiffOptionsColor = (arg: string) => string;
|
1
63
|
interface DiffOptions {
|
2
|
-
|
64
|
+
aAnnotation?: string;
|
65
|
+
aColor?: DiffOptionsColor;
|
66
|
+
aIndicator?: string;
|
67
|
+
bAnnotation?: string;
|
68
|
+
bColor?: DiffOptionsColor;
|
69
|
+
bIndicator?: string;
|
70
|
+
changeColor?: DiffOptionsColor;
|
71
|
+
changeLineTrailingSpaceColor?: DiffOptionsColor;
|
72
|
+
commonColor?: DiffOptionsColor;
|
73
|
+
commonIndicator?: string;
|
74
|
+
commonLineTrailingSpaceColor?: DiffOptionsColor;
|
75
|
+
contextLines?: number;
|
76
|
+
emptyFirstOrLastLinePlaceholder?: string;
|
77
|
+
expand?: boolean;
|
78
|
+
includeChangeCounts?: boolean;
|
79
|
+
omitAnnotationLines?: boolean;
|
80
|
+
patchColor?: DiffOptionsColor;
|
81
|
+
compareKeys?: CompareKeys;
|
3
82
|
}
|
83
|
+
|
84
|
+
/**
|
85
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
86
|
+
*
|
87
|
+
* This source code is licensed under the MIT license found in the
|
88
|
+
* LICENSE file in the root directory of this source tree.
|
89
|
+
*/
|
90
|
+
|
91
|
+
declare function diffLinesUnified(aLines: Array<string>, bLines: Array<string>, options?: DiffOptions): string;
|
92
|
+
declare function diffLinesUnified2(aLinesDisplay: Array<string>, bLinesDisplay: Array<string>, aLinesCompare: Array<string>, bLinesCompare: Array<string>, options?: DiffOptions): string;
|
93
|
+
declare function diffLinesRaw(aLines: Array<string>, bLines: Array<string>): Array<Diff>;
|
94
|
+
|
4
95
|
/**
|
5
|
-
*
|
96
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
97
|
+
*
|
98
|
+
* This source code is licensed under the MIT license found in the
|
99
|
+
* LICENSE file in the root directory of this source tree.
|
100
|
+
*/
|
101
|
+
|
102
|
+
declare function diffStringsUnified(a: string, b: string, options?: DiffOptions): string;
|
103
|
+
declare function diffStringsRaw(a: string, b: string, cleanup: boolean): Array<Diff>;
|
104
|
+
|
105
|
+
/**
|
106
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
6
107
|
*
|
7
|
-
*
|
8
|
-
*
|
9
|
-
* @param {String} expected
|
10
|
-
* @return {string} The diff.
|
108
|
+
* This source code is licensed under the MIT license found in the
|
109
|
+
* LICENSE file in the root directory of this source tree.
|
11
110
|
*/
|
12
|
-
declare function unifiedDiff(actual: unknown, expected: unknown, options?: DiffOptions): string;
|
13
111
|
|
14
|
-
|
112
|
+
declare function diff(a: any, b: any, options?: DiffOptions): string | null;
|
113
|
+
|
114
|
+
export { DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, Diff, DiffOptions, DiffOptionsColor, diff, diffLinesRaw, diffLinesUnified, diffLinesUnified2, diffStringsRaw, diffStringsUnified };
|
package/dist/diff.js
CHANGED
@@ -1,118 +1,1059 @@
|
|
1
|
+
import { format, plugins } from 'pretty-format';
|
2
|
+
import * as diff$1 from 'diff-sequences';
|
1
3
|
import { b as getColors } from './chunk-colors.js';
|
2
|
-
import * as concordance from 'concordance';
|
3
4
|
|
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
|
-
|
5
|
+
function getType(value) {
|
6
|
+
if (value === void 0) {
|
7
|
+
return "undefined";
|
8
|
+
} else if (value === null) {
|
9
|
+
return "null";
|
10
|
+
} else if (Array.isArray(value)) {
|
11
|
+
return "array";
|
12
|
+
} else if (typeof value === "boolean") {
|
13
|
+
return "boolean";
|
14
|
+
} else if (typeof value === "function") {
|
15
|
+
return "function";
|
16
|
+
} else if (typeof value === "number") {
|
17
|
+
return "number";
|
18
|
+
} else if (typeof value === "string") {
|
19
|
+
return "string";
|
20
|
+
} else if (typeof value === "bigint") {
|
21
|
+
return "bigint";
|
22
|
+
} else if (typeof value === "object") {
|
23
|
+
if (value != null) {
|
24
|
+
if (value.constructor === RegExp)
|
25
|
+
return "regexp";
|
26
|
+
else if (value.constructor === Map)
|
27
|
+
return "map";
|
28
|
+
else if (value.constructor === Set)
|
29
|
+
return "set";
|
30
|
+
else if (value.constructor === Date)
|
31
|
+
return "date";
|
32
|
+
}
|
33
|
+
return "object";
|
34
|
+
} else if (typeof value === "symbol") {
|
35
|
+
return "symbol";
|
36
|
+
}
|
37
|
+
throw new Error(`value of unknown type: ${value}`);
|
38
|
+
}
|
39
|
+
|
40
|
+
const DIFF_DELETE = -1;
|
41
|
+
const DIFF_INSERT = 1;
|
42
|
+
const DIFF_EQUAL = 0;
|
43
|
+
class Diff {
|
44
|
+
constructor(op, text) {
|
45
|
+
this[0] = op;
|
46
|
+
this[1] = text;
|
47
|
+
}
|
48
|
+
}
|
49
|
+
const diff_commonPrefix = function(text1, text2) {
|
50
|
+
if (!text1 || !text2 || text1.charAt(0) !== text2.charAt(0))
|
51
|
+
return 0;
|
52
|
+
let pointermin = 0;
|
53
|
+
let pointermax = Math.min(text1.length, text2.length);
|
54
|
+
let pointermid = pointermax;
|
55
|
+
let pointerstart = 0;
|
56
|
+
while (pointermin < pointermid) {
|
57
|
+
if (text1.substring(pointerstart, pointermid) === text2.substring(pointerstart, pointermid)) {
|
58
|
+
pointermin = pointermid;
|
59
|
+
pointerstart = pointermin;
|
60
|
+
} else {
|
61
|
+
pointermax = pointermid;
|
62
|
+
}
|
63
|
+
pointermid = Math.floor((pointermax - pointermin) / 2 + pointermin);
|
64
|
+
}
|
65
|
+
return pointermid;
|
66
|
+
};
|
67
|
+
const diff_commonSuffix = function(text1, text2) {
|
68
|
+
if (!text1 || !text2 || text1.charAt(text1.length - 1) !== text2.charAt(text2.length - 1))
|
69
|
+
return 0;
|
70
|
+
let pointermin = 0;
|
71
|
+
let pointermax = Math.min(text1.length, text2.length);
|
72
|
+
let pointermid = pointermax;
|
73
|
+
let pointerend = 0;
|
74
|
+
while (pointermin < pointermid) {
|
75
|
+
if (text1.substring(text1.length - pointermid, text1.length - pointerend) === text2.substring(text2.length - pointermid, text2.length - pointerend)) {
|
76
|
+
pointermin = pointermid;
|
77
|
+
pointerend = pointermin;
|
78
|
+
} else {
|
79
|
+
pointermax = pointermid;
|
80
|
+
}
|
81
|
+
pointermid = Math.floor((pointermax - pointermin) / 2 + pointermin);
|
82
|
+
}
|
83
|
+
return pointermid;
|
84
|
+
};
|
85
|
+
const diff_commonOverlap_ = function(text1, text2) {
|
86
|
+
const text1_length = text1.length;
|
87
|
+
const text2_length = text2.length;
|
88
|
+
if (text1_length === 0 || text2_length === 0)
|
89
|
+
return 0;
|
90
|
+
if (text1_length > text2_length)
|
91
|
+
text1 = text1.substring(text1_length - text2_length);
|
92
|
+
else if (text1_length < text2_length)
|
93
|
+
text2 = text2.substring(0, text1_length);
|
94
|
+
const text_length = Math.min(text1_length, text2_length);
|
95
|
+
if (text1 === text2)
|
96
|
+
return text_length;
|
97
|
+
let best = 0;
|
98
|
+
let length = 1;
|
99
|
+
while (true) {
|
100
|
+
const pattern = text1.substring(text_length - length);
|
101
|
+
const found = text2.indexOf(pattern);
|
102
|
+
if (found === -1)
|
103
|
+
return best;
|
104
|
+
length += found;
|
105
|
+
if (found === 0 || text1.substring(text_length - length) === text2.substring(0, length)) {
|
106
|
+
best = length;
|
107
|
+
length++;
|
108
|
+
}
|
109
|
+
}
|
110
|
+
};
|
111
|
+
const diff_cleanupSemantic = function(diffs) {
|
112
|
+
let changes = false;
|
113
|
+
const equalities = [];
|
114
|
+
let equalitiesLength = 0;
|
115
|
+
let lastEquality = null;
|
116
|
+
let pointer = 0;
|
117
|
+
let length_insertions1 = 0;
|
118
|
+
let length_deletions1 = 0;
|
119
|
+
let length_insertions2 = 0;
|
120
|
+
let length_deletions2 = 0;
|
121
|
+
while (pointer < diffs.length) {
|
122
|
+
if (diffs[pointer][0] === DIFF_EQUAL) {
|
123
|
+
equalities[equalitiesLength++] = pointer;
|
124
|
+
length_insertions1 = length_insertions2;
|
125
|
+
length_deletions1 = length_deletions2;
|
126
|
+
length_insertions2 = 0;
|
127
|
+
length_deletions2 = 0;
|
128
|
+
lastEquality = diffs[pointer][1];
|
129
|
+
} else {
|
130
|
+
if (diffs[pointer][0] === DIFF_INSERT)
|
131
|
+
length_insertions2 += diffs[pointer][1].length;
|
132
|
+
else
|
133
|
+
length_deletions2 += diffs[pointer][1].length;
|
134
|
+
if (lastEquality && lastEquality.length <= Math.max(length_insertions1, length_deletions1) && lastEquality.length <= Math.max(
|
135
|
+
length_insertions2,
|
136
|
+
length_deletions2
|
137
|
+
)) {
|
138
|
+
diffs.splice(
|
139
|
+
equalities[equalitiesLength - 1],
|
140
|
+
0,
|
141
|
+
new Diff(DIFF_DELETE, lastEquality)
|
142
|
+
);
|
143
|
+
diffs[equalities[equalitiesLength - 1] + 1][0] = DIFF_INSERT;
|
144
|
+
equalitiesLength--;
|
145
|
+
equalitiesLength--;
|
146
|
+
pointer = equalitiesLength > 0 ? equalities[equalitiesLength - 1] : -1;
|
147
|
+
length_insertions1 = 0;
|
148
|
+
length_deletions1 = 0;
|
149
|
+
length_insertions2 = 0;
|
150
|
+
length_deletions2 = 0;
|
151
|
+
lastEquality = null;
|
152
|
+
changes = true;
|
153
|
+
}
|
154
|
+
}
|
155
|
+
pointer++;
|
156
|
+
}
|
157
|
+
if (changes)
|
158
|
+
diff_cleanupMerge(diffs);
|
159
|
+
diff_cleanupSemanticLossless(diffs);
|
160
|
+
pointer = 1;
|
161
|
+
while (pointer < diffs.length) {
|
162
|
+
if (diffs[pointer - 1][0] === DIFF_DELETE && diffs[pointer][0] === DIFF_INSERT) {
|
163
|
+
const deletion = diffs[pointer - 1][1];
|
164
|
+
const insertion = diffs[pointer][1];
|
165
|
+
const overlap_length1 = diff_commonOverlap_(deletion, insertion);
|
166
|
+
const overlap_length2 = diff_commonOverlap_(insertion, deletion);
|
167
|
+
if (overlap_length1 >= overlap_length2) {
|
168
|
+
if (overlap_length1 >= deletion.length / 2 || overlap_length1 >= insertion.length / 2) {
|
169
|
+
diffs.splice(pointer, 0, new Diff(
|
170
|
+
DIFF_EQUAL,
|
171
|
+
insertion.substring(0, overlap_length1)
|
172
|
+
));
|
173
|
+
diffs[pointer - 1][1] = deletion.substring(0, deletion.length - overlap_length1);
|
174
|
+
diffs[pointer + 1][1] = insertion.substring(overlap_length1);
|
175
|
+
pointer++;
|
176
|
+
}
|
177
|
+
} else {
|
178
|
+
if (overlap_length2 >= deletion.length / 2 || overlap_length2 >= insertion.length / 2) {
|
179
|
+
diffs.splice(pointer, 0, new Diff(
|
180
|
+
DIFF_EQUAL,
|
181
|
+
deletion.substring(0, overlap_length2)
|
182
|
+
));
|
183
|
+
diffs[pointer - 1][0] = DIFF_INSERT;
|
184
|
+
diffs[pointer - 1][1] = insertion.substring(0, insertion.length - overlap_length2);
|
185
|
+
diffs[pointer + 1][0] = DIFF_DELETE;
|
186
|
+
diffs[pointer + 1][1] = deletion.substring(overlap_length2);
|
187
|
+
pointer++;
|
74
188
|
}
|
75
189
|
}
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
190
|
+
pointer++;
|
191
|
+
}
|
192
|
+
pointer++;
|
193
|
+
}
|
194
|
+
};
|
195
|
+
const nonAlphaNumericRegex_ = /[^a-zA-Z0-9]/;
|
196
|
+
const whitespaceRegex_ = /\s/;
|
197
|
+
const linebreakRegex_ = /[\r\n]/;
|
198
|
+
const blanklineEndRegex_ = /\n\r?\n$/;
|
199
|
+
const blanklineStartRegex_ = /^\r?\n\r?\n/;
|
200
|
+
function diff_cleanupSemanticLossless(diffs) {
|
201
|
+
function diff_cleanupSemanticScore_(one, two) {
|
202
|
+
if (!one || !two) {
|
203
|
+
return 6;
|
204
|
+
}
|
205
|
+
const char1 = one.charAt(one.length - 1);
|
206
|
+
const char2 = two.charAt(0);
|
207
|
+
const nonAlphaNumeric1 = char1.match(nonAlphaNumericRegex_);
|
208
|
+
const nonAlphaNumeric2 = char2.match(nonAlphaNumericRegex_);
|
209
|
+
const whitespace1 = nonAlphaNumeric1 && char1.match(whitespaceRegex_);
|
210
|
+
const whitespace2 = nonAlphaNumeric2 && char2.match(whitespaceRegex_);
|
211
|
+
const lineBreak1 = whitespace1 && char1.match(linebreakRegex_);
|
212
|
+
const lineBreak2 = whitespace2 && char2.match(linebreakRegex_);
|
213
|
+
const blankLine1 = lineBreak1 && one.match(blanklineEndRegex_);
|
214
|
+
const blankLine2 = lineBreak2 && two.match(blanklineStartRegex_);
|
215
|
+
if (blankLine1 || blankLine2) {
|
216
|
+
return 5;
|
217
|
+
} else if (lineBreak1 || lineBreak2) {
|
218
|
+
return 4;
|
219
|
+
} else if (nonAlphaNumeric1 && !whitespace1 && whitespace2) {
|
220
|
+
return 3;
|
221
|
+
} else if (whitespace1 || whitespace2) {
|
222
|
+
return 2;
|
223
|
+
} else if (nonAlphaNumeric1 || nonAlphaNumeric2) {
|
224
|
+
return 1;
|
225
|
+
}
|
226
|
+
return 0;
|
227
|
+
}
|
228
|
+
let pointer = 1;
|
229
|
+
while (pointer < diffs.length - 1) {
|
230
|
+
if (diffs[pointer - 1][0] === DIFF_EQUAL && diffs[pointer + 1][0] === DIFF_EQUAL) {
|
231
|
+
let equality1 = diffs[pointer - 1][1];
|
232
|
+
let edit = diffs[pointer][1];
|
233
|
+
let equality2 = diffs[pointer + 1][1];
|
234
|
+
const commonOffset = diff_commonSuffix(equality1, edit);
|
235
|
+
if (commonOffset) {
|
236
|
+
const commonString = edit.substring(edit.length - commonOffset);
|
237
|
+
equality1 = equality1.substring(0, equality1.length - commonOffset);
|
238
|
+
edit = commonString + edit.substring(0, edit.length - commonOffset);
|
239
|
+
equality2 = commonString + equality2;
|
240
|
+
}
|
241
|
+
let bestEquality1 = equality1;
|
242
|
+
let bestEdit = edit;
|
243
|
+
let bestEquality2 = equality2;
|
244
|
+
let bestScore = diff_cleanupSemanticScore_(equality1, edit) + diff_cleanupSemanticScore_(edit, equality2);
|
245
|
+
while (edit.charAt(0) === equality2.charAt(0)) {
|
246
|
+
equality1 += edit.charAt(0);
|
247
|
+
edit = edit.substring(1) + equality2.charAt(0);
|
248
|
+
equality2 = equality2.substring(1);
|
249
|
+
const score = diff_cleanupSemanticScore_(equality1, edit) + diff_cleanupSemanticScore_(edit, equality2);
|
250
|
+
if (score >= bestScore) {
|
251
|
+
bestScore = score;
|
252
|
+
bestEquality1 = equality1;
|
253
|
+
bestEdit = edit;
|
254
|
+
bestEquality2 = equality2;
|
255
|
+
}
|
256
|
+
}
|
257
|
+
if (diffs[pointer - 1][1] !== bestEquality1) {
|
258
|
+
if (bestEquality1) {
|
259
|
+
diffs[pointer - 1][1] = bestEquality1;
|
260
|
+
} else {
|
261
|
+
diffs.splice(pointer - 1, 1);
|
262
|
+
pointer--;
|
263
|
+
}
|
264
|
+
diffs[pointer][1] = bestEdit;
|
265
|
+
if (bestEquality2) {
|
266
|
+
diffs[pointer + 1][1] = bestEquality2;
|
267
|
+
} else {
|
268
|
+
diffs.splice(pointer + 1, 1);
|
269
|
+
pointer--;
|
270
|
+
}
|
271
|
+
}
|
272
|
+
}
|
273
|
+
pointer++;
|
274
|
+
}
|
83
275
|
}
|
84
|
-
function
|
85
|
-
|
276
|
+
function diff_cleanupMerge(diffs) {
|
277
|
+
diffs.push(new Diff(DIFF_EQUAL, ""));
|
278
|
+
let pointer = 0;
|
279
|
+
let count_delete = 0;
|
280
|
+
let count_insert = 0;
|
281
|
+
let text_delete = "";
|
282
|
+
let text_insert = "";
|
283
|
+
let commonlength;
|
284
|
+
while (pointer < diffs.length) {
|
285
|
+
switch (diffs[pointer][0]) {
|
286
|
+
case DIFF_INSERT:
|
287
|
+
count_insert++;
|
288
|
+
text_insert += diffs[pointer][1];
|
289
|
+
pointer++;
|
290
|
+
break;
|
291
|
+
case DIFF_DELETE:
|
292
|
+
count_delete++;
|
293
|
+
text_delete += diffs[pointer][1];
|
294
|
+
pointer++;
|
295
|
+
break;
|
296
|
+
case DIFF_EQUAL:
|
297
|
+
if (count_delete + count_insert > 1) {
|
298
|
+
if (count_delete !== 0 && count_insert !== 0) {
|
299
|
+
commonlength = diff_commonPrefix(text_insert, text_delete);
|
300
|
+
if (commonlength !== 0) {
|
301
|
+
if (pointer - count_delete - count_insert > 0 && diffs[pointer - count_delete - count_insert - 1][0] === DIFF_EQUAL) {
|
302
|
+
diffs[pointer - count_delete - count_insert - 1][1] += text_insert.substring(0, commonlength);
|
303
|
+
} else {
|
304
|
+
diffs.splice(0, 0, new Diff(
|
305
|
+
DIFF_EQUAL,
|
306
|
+
text_insert.substring(0, commonlength)
|
307
|
+
));
|
308
|
+
pointer++;
|
309
|
+
}
|
310
|
+
text_insert = text_insert.substring(commonlength);
|
311
|
+
text_delete = text_delete.substring(commonlength);
|
312
|
+
}
|
313
|
+
commonlength = diff_commonSuffix(text_insert, text_delete);
|
314
|
+
if (commonlength !== 0) {
|
315
|
+
diffs[pointer][1] = text_insert.substring(text_insert.length - commonlength) + diffs[pointer][1];
|
316
|
+
text_insert = text_insert.substring(0, text_insert.length - commonlength);
|
317
|
+
text_delete = text_delete.substring(0, text_delete.length - commonlength);
|
318
|
+
}
|
319
|
+
}
|
320
|
+
pointer -= count_delete + count_insert;
|
321
|
+
diffs.splice(pointer, count_delete + count_insert);
|
322
|
+
if (text_delete.length) {
|
323
|
+
diffs.splice(
|
324
|
+
pointer,
|
325
|
+
0,
|
326
|
+
new Diff(DIFF_DELETE, text_delete)
|
327
|
+
);
|
328
|
+
pointer++;
|
329
|
+
}
|
330
|
+
if (text_insert.length) {
|
331
|
+
diffs.splice(
|
332
|
+
pointer,
|
333
|
+
0,
|
334
|
+
new Diff(DIFF_INSERT, text_insert)
|
335
|
+
);
|
336
|
+
pointer++;
|
337
|
+
}
|
338
|
+
pointer++;
|
339
|
+
} else if (pointer !== 0 && diffs[pointer - 1][0] === DIFF_EQUAL) {
|
340
|
+
diffs[pointer - 1][1] += diffs[pointer][1];
|
341
|
+
diffs.splice(pointer, 1);
|
342
|
+
} else {
|
343
|
+
pointer++;
|
344
|
+
}
|
345
|
+
count_insert = 0;
|
346
|
+
count_delete = 0;
|
347
|
+
text_delete = "";
|
348
|
+
text_insert = "";
|
349
|
+
break;
|
350
|
+
}
|
351
|
+
}
|
352
|
+
if (diffs[diffs.length - 1][1] === "")
|
353
|
+
diffs.pop();
|
354
|
+
let changes = false;
|
355
|
+
pointer = 1;
|
356
|
+
while (pointer < diffs.length - 1) {
|
357
|
+
if (diffs[pointer - 1][0] === DIFF_EQUAL && diffs[pointer + 1][0] === DIFF_EQUAL) {
|
358
|
+
if (diffs[pointer][1].substring(diffs[pointer][1].length - diffs[pointer - 1][1].length) === diffs[pointer - 1][1]) {
|
359
|
+
diffs[pointer][1] = diffs[pointer - 1][1] + diffs[pointer][1].substring(0, diffs[pointer][1].length - diffs[pointer - 1][1].length);
|
360
|
+
diffs[pointer + 1][1] = diffs[pointer - 1][1] + diffs[pointer + 1][1];
|
361
|
+
diffs.splice(pointer - 1, 1);
|
362
|
+
changes = true;
|
363
|
+
} else if (diffs[pointer][1].substring(0, diffs[pointer + 1][1].length) === diffs[pointer + 1][1]) {
|
364
|
+
diffs[pointer - 1][1] += diffs[pointer + 1][1];
|
365
|
+
diffs[pointer][1] = diffs[pointer][1].substring(diffs[pointer + 1][1].length) + diffs[pointer + 1][1];
|
366
|
+
diffs.splice(pointer + 1, 1);
|
367
|
+
changes = true;
|
368
|
+
}
|
369
|
+
}
|
370
|
+
pointer++;
|
371
|
+
}
|
372
|
+
if (changes)
|
373
|
+
diff_cleanupMerge(diffs);
|
86
374
|
}
|
87
375
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
376
|
+
const NO_DIFF_MESSAGE = "Compared values have no visual difference.";
|
377
|
+
const SIMILAR_MESSAGE = "Compared values serialize to the same structure.\nPrinting internal object structure without calling `toJSON` instead.";
|
378
|
+
|
379
|
+
function formatTrailingSpaces(line, trailingSpaceFormatter) {
|
380
|
+
return line.replace(/\s+$/, (match) => trailingSpaceFormatter(match));
|
381
|
+
}
|
382
|
+
function printDiffLine(line, isFirstOrLast, color, indicator, trailingSpaceFormatter, emptyFirstOrLastLinePlaceholder) {
|
383
|
+
return line.length !== 0 ? color(
|
384
|
+
`${indicator} ${formatTrailingSpaces(line, trailingSpaceFormatter)}`
|
385
|
+
) : indicator !== " " ? color(indicator) : isFirstOrLast && emptyFirstOrLastLinePlaceholder.length !== 0 ? color(`${indicator} ${emptyFirstOrLastLinePlaceholder}`) : "";
|
386
|
+
}
|
387
|
+
function printDeleteLine(line, isFirstOrLast, {
|
388
|
+
aColor,
|
389
|
+
aIndicator,
|
390
|
+
changeLineTrailingSpaceColor,
|
391
|
+
emptyFirstOrLastLinePlaceholder
|
392
|
+
}) {
|
393
|
+
return printDiffLine(
|
394
|
+
line,
|
395
|
+
isFirstOrLast,
|
396
|
+
aColor,
|
397
|
+
aIndicator,
|
398
|
+
changeLineTrailingSpaceColor,
|
399
|
+
emptyFirstOrLastLinePlaceholder
|
400
|
+
);
|
401
|
+
}
|
402
|
+
function printInsertLine(line, isFirstOrLast, {
|
403
|
+
bColor,
|
404
|
+
bIndicator,
|
405
|
+
changeLineTrailingSpaceColor,
|
406
|
+
emptyFirstOrLastLinePlaceholder
|
407
|
+
}) {
|
408
|
+
return printDiffLine(
|
409
|
+
line,
|
410
|
+
isFirstOrLast,
|
411
|
+
bColor,
|
412
|
+
bIndicator,
|
413
|
+
changeLineTrailingSpaceColor,
|
414
|
+
emptyFirstOrLastLinePlaceholder
|
415
|
+
);
|
416
|
+
}
|
417
|
+
function printCommonLine(line, isFirstOrLast, {
|
418
|
+
commonColor,
|
419
|
+
commonIndicator,
|
420
|
+
commonLineTrailingSpaceColor,
|
421
|
+
emptyFirstOrLastLinePlaceholder
|
422
|
+
}) {
|
423
|
+
return printDiffLine(
|
424
|
+
line,
|
425
|
+
isFirstOrLast,
|
426
|
+
commonColor,
|
427
|
+
commonIndicator,
|
428
|
+
commonLineTrailingSpaceColor,
|
429
|
+
emptyFirstOrLastLinePlaceholder
|
430
|
+
);
|
431
|
+
}
|
432
|
+
function createPatchMark(aStart, aEnd, bStart, bEnd, { patchColor }) {
|
433
|
+
return patchColor(
|
434
|
+
`@@ -${aStart + 1},${aEnd - aStart} +${bStart + 1},${bEnd - bStart} @@`
|
435
|
+
);
|
436
|
+
}
|
437
|
+
function joinAlignedDiffsNoExpand(diffs, options) {
|
438
|
+
const iLength = diffs.length;
|
439
|
+
const nContextLines = options.contextLines;
|
440
|
+
const nContextLines2 = nContextLines + nContextLines;
|
441
|
+
let jLength = iLength;
|
442
|
+
let hasExcessAtStartOrEnd = false;
|
443
|
+
let nExcessesBetweenChanges = 0;
|
444
|
+
let i = 0;
|
445
|
+
while (i !== iLength) {
|
446
|
+
const iStart = i;
|
447
|
+
while (i !== iLength && diffs[i][0] === DIFF_EQUAL)
|
448
|
+
i += 1;
|
449
|
+
if (iStart !== i) {
|
450
|
+
if (iStart === 0) {
|
451
|
+
if (i > nContextLines) {
|
452
|
+
jLength -= i - nContextLines;
|
453
|
+
hasExcessAtStartOrEnd = true;
|
454
|
+
}
|
455
|
+
} else if (i === iLength) {
|
456
|
+
const n = i - iStart;
|
457
|
+
if (n > nContextLines) {
|
458
|
+
jLength -= n - nContextLines;
|
459
|
+
hasExcessAtStartOrEnd = true;
|
460
|
+
}
|
461
|
+
} else {
|
462
|
+
const n = i - iStart;
|
463
|
+
if (n > nContextLines2) {
|
464
|
+
jLength -= n - nContextLines2;
|
465
|
+
nExcessesBetweenChanges += 1;
|
466
|
+
}
|
467
|
+
}
|
468
|
+
}
|
469
|
+
while (i !== iLength && diffs[i][0] !== DIFF_EQUAL)
|
470
|
+
i += 1;
|
471
|
+
}
|
472
|
+
const hasPatch = nExcessesBetweenChanges !== 0 || hasExcessAtStartOrEnd;
|
473
|
+
if (nExcessesBetweenChanges !== 0)
|
474
|
+
jLength += nExcessesBetweenChanges + 1;
|
475
|
+
else if (hasExcessAtStartOrEnd)
|
476
|
+
jLength += 1;
|
477
|
+
const jLast = jLength - 1;
|
478
|
+
const lines = [];
|
479
|
+
let jPatchMark = 0;
|
480
|
+
if (hasPatch)
|
481
|
+
lines.push("");
|
482
|
+
let aStart = 0;
|
483
|
+
let bStart = 0;
|
484
|
+
let aEnd = 0;
|
485
|
+
let bEnd = 0;
|
486
|
+
const pushCommonLine = (line) => {
|
487
|
+
const j = lines.length;
|
488
|
+
lines.push(printCommonLine(line, j === 0 || j === jLast, options));
|
489
|
+
aEnd += 1;
|
490
|
+
bEnd += 1;
|
491
|
+
};
|
492
|
+
const pushDeleteLine = (line) => {
|
493
|
+
const j = lines.length;
|
494
|
+
lines.push(printDeleteLine(line, j === 0 || j === jLast, options));
|
495
|
+
aEnd += 1;
|
95
496
|
};
|
497
|
+
const pushInsertLine = (line) => {
|
498
|
+
const j = lines.length;
|
499
|
+
lines.push(printInsertLine(line, j === 0 || j === jLast, options));
|
500
|
+
bEnd += 1;
|
501
|
+
};
|
502
|
+
i = 0;
|
503
|
+
while (i !== iLength) {
|
504
|
+
let iStart = i;
|
505
|
+
while (i !== iLength && diffs[i][0] === DIFF_EQUAL)
|
506
|
+
i += 1;
|
507
|
+
if (iStart !== i) {
|
508
|
+
if (iStart === 0) {
|
509
|
+
if (i > nContextLines) {
|
510
|
+
iStart = i - nContextLines;
|
511
|
+
aStart = iStart;
|
512
|
+
bStart = iStart;
|
513
|
+
aEnd = aStart;
|
514
|
+
bEnd = bStart;
|
515
|
+
}
|
516
|
+
for (let iCommon = iStart; iCommon !== i; iCommon += 1)
|
517
|
+
pushCommonLine(diffs[iCommon][1]);
|
518
|
+
} else if (i === iLength) {
|
519
|
+
const iEnd = i - iStart > nContextLines ? iStart + nContextLines : i;
|
520
|
+
for (let iCommon = iStart; iCommon !== iEnd; iCommon += 1)
|
521
|
+
pushCommonLine(diffs[iCommon][1]);
|
522
|
+
} else {
|
523
|
+
const nCommon = i - iStart;
|
524
|
+
if (nCommon > nContextLines2) {
|
525
|
+
const iEnd = iStart + nContextLines;
|
526
|
+
for (let iCommon = iStart; iCommon !== iEnd; iCommon += 1)
|
527
|
+
pushCommonLine(diffs[iCommon][1]);
|
528
|
+
lines[jPatchMark] = createPatchMark(
|
529
|
+
aStart,
|
530
|
+
aEnd,
|
531
|
+
bStart,
|
532
|
+
bEnd,
|
533
|
+
options
|
534
|
+
);
|
535
|
+
jPatchMark = lines.length;
|
536
|
+
lines.push("");
|
537
|
+
const nOmit = nCommon - nContextLines2;
|
538
|
+
aStart = aEnd + nOmit;
|
539
|
+
bStart = bEnd + nOmit;
|
540
|
+
aEnd = aStart;
|
541
|
+
bEnd = bStart;
|
542
|
+
for (let iCommon = i - nContextLines; iCommon !== i; iCommon += 1)
|
543
|
+
pushCommonLine(diffs[iCommon][1]);
|
544
|
+
} else {
|
545
|
+
for (let iCommon = iStart; iCommon !== i; iCommon += 1)
|
546
|
+
pushCommonLine(diffs[iCommon][1]);
|
547
|
+
}
|
548
|
+
}
|
549
|
+
}
|
550
|
+
while (i !== iLength && diffs[i][0] === DIFF_DELETE) {
|
551
|
+
pushDeleteLine(diffs[i][1]);
|
552
|
+
i += 1;
|
553
|
+
}
|
554
|
+
while (i !== iLength && diffs[i][0] === DIFF_INSERT) {
|
555
|
+
pushInsertLine(diffs[i][1]);
|
556
|
+
i += 1;
|
557
|
+
}
|
558
|
+
}
|
559
|
+
if (hasPatch)
|
560
|
+
lines[jPatchMark] = createPatchMark(aStart, aEnd, bStart, bEnd, options);
|
561
|
+
return lines.join("\n");
|
562
|
+
}
|
563
|
+
function joinAlignedDiffsExpand(diffs, options) {
|
564
|
+
return diffs.map((diff, i, diffs2) => {
|
565
|
+
const line = diff[1];
|
566
|
+
const isFirstOrLast = i === 0 || i === diffs2.length - 1;
|
567
|
+
switch (diff[0]) {
|
568
|
+
case DIFF_DELETE:
|
569
|
+
return printDeleteLine(line, isFirstOrLast, options);
|
570
|
+
case DIFF_INSERT:
|
571
|
+
return printInsertLine(line, isFirstOrLast, options);
|
572
|
+
default:
|
573
|
+
return printCommonLine(line, isFirstOrLast, options);
|
574
|
+
}
|
575
|
+
}).join("\n");
|
576
|
+
}
|
577
|
+
|
578
|
+
const noColor = (string) => string;
|
579
|
+
const DIFF_CONTEXT_DEFAULT = 5;
|
580
|
+
function getDefaultOptions() {
|
96
581
|
const c = getColors();
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
582
|
+
return {
|
583
|
+
aAnnotation: "Expected",
|
584
|
+
aColor: c.green,
|
585
|
+
aIndicator: "-",
|
586
|
+
bAnnotation: "Received",
|
587
|
+
bColor: c.red,
|
588
|
+
bIndicator: "+",
|
589
|
+
changeColor: c.inverse,
|
590
|
+
changeLineTrailingSpaceColor: noColor,
|
591
|
+
commonColor: c.dim,
|
592
|
+
commonIndicator: " ",
|
593
|
+
commonLineTrailingSpaceColor: noColor,
|
594
|
+
compareKeys: void 0,
|
595
|
+
contextLines: DIFF_CONTEXT_DEFAULT,
|
596
|
+
emptyFirstOrLastLinePlaceholder: "",
|
597
|
+
expand: true,
|
598
|
+
includeChangeCounts: false,
|
599
|
+
omitAnnotationLines: false,
|
600
|
+
patchColor: c.yellow
|
601
|
+
};
|
602
|
+
}
|
603
|
+
function getCompareKeys(compareKeys) {
|
604
|
+
return compareKeys && typeof compareKeys === "function" ? compareKeys : void 0;
|
605
|
+
}
|
606
|
+
function getContextLines(contextLines) {
|
607
|
+
return typeof contextLines === "number" && Number.isSafeInteger(contextLines) && contextLines >= 0 ? contextLines : DIFF_CONTEXT_DEFAULT;
|
608
|
+
}
|
609
|
+
function normalizeDiffOptions(options = {}) {
|
610
|
+
return {
|
611
|
+
...getDefaultOptions(),
|
612
|
+
...options,
|
613
|
+
compareKeys: getCompareKeys(options.compareKeys),
|
614
|
+
contextLines: getContextLines(options.contextLines)
|
615
|
+
};
|
616
|
+
}
|
617
|
+
|
618
|
+
function isEmptyString(lines) {
|
619
|
+
return lines.length === 1 && lines[0].length === 0;
|
620
|
+
}
|
621
|
+
function countChanges(diffs) {
|
622
|
+
let a = 0;
|
623
|
+
let b = 0;
|
624
|
+
diffs.forEach((diff2) => {
|
625
|
+
switch (diff2[0]) {
|
626
|
+
case DIFF_DELETE:
|
627
|
+
a += 1;
|
628
|
+
break;
|
629
|
+
case DIFF_INSERT:
|
630
|
+
b += 1;
|
631
|
+
break;
|
632
|
+
}
|
105
633
|
});
|
106
|
-
|
634
|
+
return { a, b };
|
635
|
+
}
|
636
|
+
function printAnnotation({
|
637
|
+
aAnnotation,
|
638
|
+
aColor,
|
639
|
+
aIndicator,
|
640
|
+
bAnnotation,
|
641
|
+
bColor,
|
642
|
+
bIndicator,
|
643
|
+
includeChangeCounts,
|
644
|
+
omitAnnotationLines
|
645
|
+
}, changeCounts) {
|
646
|
+
if (omitAnnotationLines)
|
107
647
|
return "";
|
108
|
-
let
|
109
|
-
|
110
|
-
|
111
|
-
|
648
|
+
let aRest = "";
|
649
|
+
let bRest = "";
|
650
|
+
if (includeChangeCounts) {
|
651
|
+
const aCount = String(changeCounts.a);
|
652
|
+
const bCount = String(changeCounts.b);
|
653
|
+
const baAnnotationLengthDiff = bAnnotation.length - aAnnotation.length;
|
654
|
+
const aAnnotationPadding = " ".repeat(Math.max(0, baAnnotationLengthDiff));
|
655
|
+
const bAnnotationPadding = " ".repeat(Math.max(0, -baAnnotationLengthDiff));
|
656
|
+
const baCountLengthDiff = bCount.length - aCount.length;
|
657
|
+
const aCountPadding = " ".repeat(Math.max(0, baCountLengthDiff));
|
658
|
+
const bCountPadding = " ".repeat(Math.max(0, -baCountLengthDiff));
|
659
|
+
aRest = `${aAnnotationPadding} ${aIndicator} ${aCountPadding}${aCount}`;
|
660
|
+
bRest = `${bAnnotationPadding} ${bIndicator} ${bCountPadding}${bCount}`;
|
661
|
+
}
|
662
|
+
const a = `${aIndicator} ${aAnnotation}${aRest}`;
|
663
|
+
const b = `${bIndicator} ${bAnnotation}${bRest}`;
|
664
|
+
return `${aColor(a)}
|
665
|
+
${bColor(b)}
|
112
666
|
|
113
667
|
`;
|
668
|
+
}
|
669
|
+
function printDiffLines(diffs, options) {
|
670
|
+
return printAnnotation(options, countChanges(diffs)) + (options.expand ? joinAlignedDiffsExpand(diffs, options) : joinAlignedDiffsNoExpand(diffs, options));
|
671
|
+
}
|
672
|
+
function diffLinesUnified(aLines, bLines, options) {
|
673
|
+
return printDiffLines(
|
674
|
+
diffLinesRaw(
|
675
|
+
isEmptyString(aLines) ? [] : aLines,
|
676
|
+
isEmptyString(bLines) ? [] : bLines
|
677
|
+
),
|
678
|
+
normalizeDiffOptions(options)
|
679
|
+
);
|
680
|
+
}
|
681
|
+
function diffLinesUnified2(aLinesDisplay, bLinesDisplay, aLinesCompare, bLinesCompare, options) {
|
682
|
+
if (isEmptyString(aLinesDisplay) && isEmptyString(aLinesCompare)) {
|
683
|
+
aLinesDisplay = [];
|
684
|
+
aLinesCompare = [];
|
685
|
+
}
|
686
|
+
if (isEmptyString(bLinesDisplay) && isEmptyString(bLinesCompare)) {
|
687
|
+
bLinesDisplay = [];
|
688
|
+
bLinesCompare = [];
|
689
|
+
}
|
690
|
+
if (aLinesDisplay.length !== aLinesCompare.length || bLinesDisplay.length !== bLinesCompare.length) {
|
691
|
+
return diffLinesUnified(aLinesDisplay, bLinesDisplay, options);
|
692
|
+
}
|
693
|
+
const diffs = diffLinesRaw(aLinesCompare, bLinesCompare);
|
694
|
+
let aIndex = 0;
|
695
|
+
let bIndex = 0;
|
696
|
+
diffs.forEach((diff2) => {
|
697
|
+
switch (diff2[0]) {
|
698
|
+
case DIFF_DELETE:
|
699
|
+
diff2[1] = aLinesDisplay[aIndex];
|
700
|
+
aIndex += 1;
|
701
|
+
break;
|
702
|
+
case DIFF_INSERT:
|
703
|
+
diff2[1] = bLinesDisplay[bIndex];
|
704
|
+
bIndex += 1;
|
705
|
+
break;
|
706
|
+
default:
|
707
|
+
diff2[1] = bLinesDisplay[bIndex];
|
708
|
+
aIndex += 1;
|
709
|
+
bIndex += 1;
|
710
|
+
}
|
711
|
+
});
|
712
|
+
return printDiffLines(diffs, normalizeDiffOptions(options));
|
713
|
+
}
|
714
|
+
function diffLinesRaw(aLines, bLines) {
|
715
|
+
const aLength = aLines.length;
|
716
|
+
const bLength = bLines.length;
|
717
|
+
const isCommon = (aIndex2, bIndex2) => aLines[aIndex2] === bLines[bIndex2];
|
718
|
+
const diffs = [];
|
719
|
+
let aIndex = 0;
|
720
|
+
let bIndex = 0;
|
721
|
+
const foundSubsequence = (nCommon, aCommon, bCommon) => {
|
722
|
+
for (; aIndex !== aCommon; aIndex += 1)
|
723
|
+
diffs.push(new Diff(DIFF_DELETE, aLines[aIndex]));
|
724
|
+
for (; bIndex !== bCommon; bIndex += 1)
|
725
|
+
diffs.push(new Diff(DIFF_INSERT, bLines[bIndex]));
|
726
|
+
for (; nCommon !== 0; nCommon -= 1, aIndex += 1, bIndex += 1)
|
727
|
+
diffs.push(new Diff(DIFF_EQUAL, bLines[bIndex]));
|
728
|
+
};
|
729
|
+
diff$1.default.default(aLength, bLength, isCommon, foundSubsequence);
|
730
|
+
for (; aIndex !== aLength; aIndex += 1)
|
731
|
+
diffs.push(new Diff(DIFF_DELETE, aLines[aIndex]));
|
732
|
+
for (; bIndex !== bLength; bIndex += 1)
|
733
|
+
diffs.push(new Diff(DIFF_INSERT, bLines[bIndex]));
|
734
|
+
return diffs;
|
735
|
+
}
|
736
|
+
|
737
|
+
function diffStrings(a, b) {
|
738
|
+
const isCommon = (aIndex2, bIndex2) => a[aIndex2] === b[bIndex2];
|
739
|
+
let aIndex = 0;
|
740
|
+
let bIndex = 0;
|
741
|
+
const diffs = [];
|
742
|
+
const foundSubsequence = (nCommon, aCommon, bCommon) => {
|
743
|
+
if (aIndex !== aCommon)
|
744
|
+
diffs.push(new Diff(DIFF_DELETE, a.slice(aIndex, aCommon)));
|
745
|
+
if (bIndex !== bCommon)
|
746
|
+
diffs.push(new Diff(DIFF_INSERT, b.slice(bIndex, bCommon)));
|
747
|
+
aIndex = aCommon + nCommon;
|
748
|
+
bIndex = bCommon + nCommon;
|
749
|
+
diffs.push(new Diff(DIFF_EQUAL, b.slice(bCommon, bIndex)));
|
750
|
+
};
|
751
|
+
diff$1.default.default(a.length, b.length, isCommon, foundSubsequence);
|
752
|
+
if (aIndex !== a.length)
|
753
|
+
diffs.push(new Diff(DIFF_DELETE, a.slice(aIndex)));
|
754
|
+
if (bIndex !== b.length)
|
755
|
+
diffs.push(new Diff(DIFF_INSERT, b.slice(bIndex)));
|
756
|
+
return diffs;
|
757
|
+
}
|
758
|
+
|
759
|
+
function concatenateRelevantDiffs(op, diffs, changeColor) {
|
760
|
+
return diffs.reduce(
|
761
|
+
(reduced, diff) => reduced + (diff[0] === DIFF_EQUAL ? diff[1] : diff[0] === op && diff[1].length !== 0 ? changeColor(diff[1]) : ""),
|
762
|
+
""
|
763
|
+
);
|
764
|
+
}
|
765
|
+
class ChangeBuffer {
|
766
|
+
constructor(op, changeColor) {
|
767
|
+
this.op = op;
|
768
|
+
this.line = [];
|
769
|
+
this.lines = [];
|
770
|
+
this.changeColor = changeColor;
|
771
|
+
}
|
772
|
+
pushSubstring(substring) {
|
773
|
+
this.pushDiff(new Diff(this.op, substring));
|
774
|
+
}
|
775
|
+
pushLine() {
|
776
|
+
this.lines.push(
|
777
|
+
this.line.length !== 1 ? new Diff(
|
778
|
+
this.op,
|
779
|
+
concatenateRelevantDiffs(this.op, this.line, this.changeColor)
|
780
|
+
) : this.line[0][0] === this.op ? this.line[0] : new Diff(this.op, this.line[0][1])
|
781
|
+
// was common diff
|
782
|
+
);
|
783
|
+
this.line.length = 0;
|
784
|
+
}
|
785
|
+
isLineEmpty() {
|
786
|
+
return this.line.length === 0;
|
787
|
+
}
|
788
|
+
// Minor input to buffer.
|
789
|
+
pushDiff(diff) {
|
790
|
+
this.line.push(diff);
|
791
|
+
}
|
792
|
+
// Main input to buffer.
|
793
|
+
align(diff) {
|
794
|
+
const string = diff[1];
|
795
|
+
if (string.includes("\n")) {
|
796
|
+
const substrings = string.split("\n");
|
797
|
+
const iLast = substrings.length - 1;
|
798
|
+
substrings.forEach((substring, i) => {
|
799
|
+
if (i < iLast) {
|
800
|
+
this.pushSubstring(substring);
|
801
|
+
this.pushLine();
|
802
|
+
} else if (substring.length !== 0) {
|
803
|
+
this.pushSubstring(substring);
|
804
|
+
}
|
805
|
+
});
|
806
|
+
} else {
|
807
|
+
this.pushDiff(diff);
|
808
|
+
}
|
809
|
+
}
|
810
|
+
// Output from buffer.
|
811
|
+
moveLinesTo(lines) {
|
812
|
+
if (!this.isLineEmpty())
|
813
|
+
this.pushLine();
|
814
|
+
lines.push(...this.lines);
|
815
|
+
this.lines.length = 0;
|
816
|
+
}
|
817
|
+
}
|
818
|
+
class CommonBuffer {
|
819
|
+
constructor(deleteBuffer, insertBuffer) {
|
820
|
+
this.deleteBuffer = deleteBuffer;
|
821
|
+
this.insertBuffer = insertBuffer;
|
822
|
+
this.lines = [];
|
823
|
+
}
|
824
|
+
pushDiffCommonLine(diff) {
|
825
|
+
this.lines.push(diff);
|
826
|
+
}
|
827
|
+
pushDiffChangeLines(diff) {
|
828
|
+
const isDiffEmpty = diff[1].length === 0;
|
829
|
+
if (!isDiffEmpty || this.deleteBuffer.isLineEmpty())
|
830
|
+
this.deleteBuffer.pushDiff(diff);
|
831
|
+
if (!isDiffEmpty || this.insertBuffer.isLineEmpty())
|
832
|
+
this.insertBuffer.pushDiff(diff);
|
833
|
+
}
|
834
|
+
flushChangeLines() {
|
835
|
+
this.deleteBuffer.moveLinesTo(this.lines);
|
836
|
+
this.insertBuffer.moveLinesTo(this.lines);
|
837
|
+
}
|
838
|
+
// Input to buffer.
|
839
|
+
align(diff) {
|
840
|
+
const op = diff[0];
|
841
|
+
const string = diff[1];
|
842
|
+
if (string.includes("\n")) {
|
843
|
+
const substrings = string.split("\n");
|
844
|
+
const iLast = substrings.length - 1;
|
845
|
+
substrings.forEach((substring, i) => {
|
846
|
+
if (i === 0) {
|
847
|
+
const subdiff = new Diff(op, substring);
|
848
|
+
if (this.deleteBuffer.isLineEmpty() && this.insertBuffer.isLineEmpty()) {
|
849
|
+
this.flushChangeLines();
|
850
|
+
this.pushDiffCommonLine(subdiff);
|
851
|
+
} else {
|
852
|
+
this.pushDiffChangeLines(subdiff);
|
853
|
+
this.flushChangeLines();
|
854
|
+
}
|
855
|
+
} else if (i < iLast) {
|
856
|
+
this.pushDiffCommonLine(new Diff(op, substring));
|
857
|
+
} else if (substring.length !== 0) {
|
858
|
+
this.pushDiffChangeLines(new Diff(op, substring));
|
859
|
+
}
|
860
|
+
});
|
861
|
+
} else {
|
862
|
+
this.pushDiffChangeLines(diff);
|
863
|
+
}
|
864
|
+
}
|
865
|
+
// Output from buffer.
|
866
|
+
getLines() {
|
867
|
+
this.flushChangeLines();
|
868
|
+
return this.lines;
|
869
|
+
}
|
870
|
+
}
|
871
|
+
function getAlignedDiffs(diffs, changeColor) {
|
872
|
+
const deleteBuffer = new ChangeBuffer(DIFF_DELETE, changeColor);
|
873
|
+
const insertBuffer = new ChangeBuffer(DIFF_INSERT, changeColor);
|
874
|
+
const commonBuffer = new CommonBuffer(deleteBuffer, insertBuffer);
|
875
|
+
diffs.forEach((diff) => {
|
876
|
+
switch (diff[0]) {
|
877
|
+
case DIFF_DELETE:
|
878
|
+
deleteBuffer.align(diff);
|
879
|
+
break;
|
880
|
+
case DIFF_INSERT:
|
881
|
+
insertBuffer.align(diff);
|
882
|
+
break;
|
883
|
+
default:
|
884
|
+
commonBuffer.align(diff);
|
885
|
+
}
|
886
|
+
});
|
887
|
+
return commonBuffer.getLines();
|
888
|
+
}
|
889
|
+
|
890
|
+
function hasCommonDiff(diffs, isMultiline) {
|
891
|
+
if (isMultiline) {
|
892
|
+
const iLast = diffs.length - 1;
|
893
|
+
return diffs.some(
|
894
|
+
(diff, i) => diff[0] === DIFF_EQUAL && (i !== iLast || diff[1] !== "\n")
|
895
|
+
);
|
896
|
+
}
|
897
|
+
return diffs.some((diff) => diff[0] === DIFF_EQUAL);
|
898
|
+
}
|
899
|
+
function diffStringsUnified(a, b, options) {
|
900
|
+
if (a !== b && a.length !== 0 && b.length !== 0) {
|
901
|
+
const isMultiline = a.includes("\n") || b.includes("\n");
|
902
|
+
const diffs = diffStringsRaw(
|
903
|
+
isMultiline ? `${a}
|
904
|
+
` : a,
|
905
|
+
isMultiline ? `${b}
|
906
|
+
` : b,
|
907
|
+
true
|
908
|
+
// cleanupSemantic
|
909
|
+
);
|
910
|
+
if (hasCommonDiff(diffs, isMultiline)) {
|
911
|
+
const optionsNormalized = normalizeDiffOptions(options);
|
912
|
+
const lines = getAlignedDiffs(diffs, optionsNormalized.changeColor);
|
913
|
+
return printDiffLines(lines, optionsNormalized);
|
914
|
+
}
|
915
|
+
}
|
916
|
+
return diffLinesUnified(a.split("\n"), b.split("\n"), options);
|
917
|
+
}
|
918
|
+
function diffStringsRaw(a, b, cleanup) {
|
919
|
+
const diffs = diffStrings(a, b);
|
920
|
+
if (cleanup)
|
921
|
+
diff_cleanupSemantic(diffs);
|
922
|
+
return diffs;
|
923
|
+
}
|
924
|
+
|
925
|
+
function getCommonMessage(message, options) {
|
926
|
+
const { commonColor } = normalizeDiffOptions(options);
|
927
|
+
return commonColor(message);
|
928
|
+
}
|
929
|
+
const {
|
930
|
+
AsymmetricMatcher,
|
931
|
+
DOMCollection,
|
932
|
+
DOMElement,
|
933
|
+
Immutable,
|
934
|
+
ReactElement,
|
935
|
+
ReactTestComponent
|
936
|
+
} = plugins;
|
937
|
+
const PLUGINS = [
|
938
|
+
ReactTestComponent,
|
939
|
+
ReactElement,
|
940
|
+
DOMElement,
|
941
|
+
DOMCollection,
|
942
|
+
Immutable,
|
943
|
+
AsymmetricMatcher
|
944
|
+
];
|
945
|
+
const FORMAT_OPTIONS = {
|
946
|
+
plugins: PLUGINS
|
947
|
+
};
|
948
|
+
const FALLBACK_FORMAT_OPTIONS = {
|
949
|
+
callToJSON: false,
|
950
|
+
maxDepth: 10,
|
951
|
+
plugins: PLUGINS
|
952
|
+
};
|
953
|
+
function diff(a, b, options) {
|
954
|
+
if (Object.is(a, b))
|
955
|
+
return "";
|
956
|
+
const aType = getType(a);
|
957
|
+
let expectedType = aType;
|
958
|
+
let omitDifference = false;
|
959
|
+
if (aType === "object" && typeof a.asymmetricMatch === "function") {
|
960
|
+
if (a.$$typeof !== Symbol.for("jest.asymmetricMatcher")) {
|
961
|
+
return null;
|
962
|
+
}
|
963
|
+
if (typeof a.getExpectedType !== "function") {
|
964
|
+
return null;
|
965
|
+
}
|
966
|
+
expectedType = a.getExpectedType();
|
967
|
+
omitDifference = expectedType === "string";
|
968
|
+
}
|
969
|
+
if (expectedType !== getType(b)) {
|
970
|
+
const { aAnnotation, aColor, aIndicator, bAnnotation, bColor, bIndicator } = normalizeDiffOptions(options);
|
971
|
+
const formatOptions = getFormatOptions(FALLBACK_FORMAT_OPTIONS, options);
|
972
|
+
const aDisplay = format(a, formatOptions);
|
973
|
+
const bDisplay = format(b, formatOptions);
|
974
|
+
const aDiff = `${aColor(`${aIndicator} ${aAnnotation}:`)}
|
975
|
+
${aDisplay}`;
|
976
|
+
const bDiff = `${bColor(`${bIndicator} ${bAnnotation}:`)}
|
977
|
+
${bDisplay}`;
|
978
|
+
return `${aDiff}
|
979
|
+
|
980
|
+
${bDiff}`;
|
981
|
+
}
|
982
|
+
if (omitDifference)
|
983
|
+
return null;
|
984
|
+
switch (aType) {
|
985
|
+
case "string":
|
986
|
+
return diffLinesUnified(a.split("\n"), b.split("\n"), options);
|
987
|
+
case "boolean":
|
988
|
+
case "number":
|
989
|
+
return comparePrimitive(a, b, options);
|
990
|
+
case "map":
|
991
|
+
return compareObjects(sortMap(a), sortMap(b), options);
|
992
|
+
case "set":
|
993
|
+
return compareObjects(sortSet(a), sortSet(b), options);
|
994
|
+
default:
|
995
|
+
return compareObjects(a, b, options);
|
996
|
+
}
|
997
|
+
}
|
998
|
+
function comparePrimitive(a, b, options) {
|
999
|
+
const aFormat = format(a, FORMAT_OPTIONS);
|
1000
|
+
const bFormat = format(b, FORMAT_OPTIONS);
|
1001
|
+
return aFormat === bFormat ? "" : diffLinesUnified(aFormat.split("\n"), bFormat.split("\n"), options);
|
1002
|
+
}
|
1003
|
+
function sortMap(map) {
|
1004
|
+
return new Map(Array.from(map.entries()).sort());
|
1005
|
+
}
|
1006
|
+
function sortSet(set) {
|
1007
|
+
return new Set(Array.from(set.values()).sort());
|
1008
|
+
}
|
1009
|
+
function compareObjects(a, b, options) {
|
1010
|
+
let difference;
|
1011
|
+
let hasThrown = false;
|
1012
|
+
try {
|
1013
|
+
const formatOptions = getFormatOptions(FORMAT_OPTIONS, options);
|
1014
|
+
difference = getObjectsDifference(a, b, formatOptions, options);
|
1015
|
+
} catch {
|
1016
|
+
hasThrown = true;
|
1017
|
+
}
|
1018
|
+
const noDiffMessage = getCommonMessage(NO_DIFF_MESSAGE, options);
|
1019
|
+
if (difference === void 0 || difference === noDiffMessage) {
|
1020
|
+
const formatOptions = getFormatOptions(FALLBACK_FORMAT_OPTIONS, options);
|
1021
|
+
difference = getObjectsDifference(a, b, formatOptions, options);
|
1022
|
+
if (difference !== noDiffMessage && !hasThrown) {
|
1023
|
+
difference = `${getCommonMessage(
|
1024
|
+
SIMILAR_MESSAGE,
|
1025
|
+
options
|
1026
|
+
)}
|
1027
|
+
|
1028
|
+
${difference}`;
|
1029
|
+
}
|
1030
|
+
}
|
1031
|
+
return difference;
|
1032
|
+
}
|
1033
|
+
function getFormatOptions(formatOptions, options) {
|
1034
|
+
const { compareKeys } = normalizeDiffOptions(options);
|
1035
|
+
return {
|
1036
|
+
...formatOptions,
|
1037
|
+
compareKeys
|
1038
|
+
};
|
1039
|
+
}
|
1040
|
+
function getObjectsDifference(a, b, formatOptions, options) {
|
1041
|
+
const formatOptionsZeroIndent = { ...formatOptions, indent: 0 };
|
1042
|
+
const aCompare = format(a, formatOptionsZeroIndent);
|
1043
|
+
const bCompare = format(b, formatOptionsZeroIndent);
|
1044
|
+
if (aCompare === bCompare) {
|
1045
|
+
return getCommonMessage(NO_DIFF_MESSAGE, options);
|
1046
|
+
} else {
|
1047
|
+
const aDisplay = format(a, formatOptions);
|
1048
|
+
const bDisplay = format(b, formatOptions);
|
1049
|
+
return diffLinesUnified2(
|
1050
|
+
aDisplay.split("\n"),
|
1051
|
+
bDisplay.split("\n"),
|
1052
|
+
aCompare.split("\n"),
|
1053
|
+
bCompare.split("\n"),
|
1054
|
+
options
|
1055
|
+
);
|
114
1056
|
}
|
115
|
-
return legend + diff.replace(/␊\s*$/mg, "");
|
116
1057
|
}
|
117
1058
|
|
118
|
-
export {
|
1059
|
+
export { DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, Diff, diff, diffLinesRaw, diffLinesUnified, diffLinesUnified2, diffStringsRaw, diffStringsUnified };
|
package/dist/error.d.ts
CHANGED
@@ -1,7 +1,5 @@
|
|
1
|
-
import { DiffOptions } from './diff.js';
|
2
|
-
|
3
1
|
declare function serializeError(val: any, seen?: WeakMap<object, any>): any;
|
4
|
-
declare function processError(err: any
|
2
|
+
declare function processError(err: any): any;
|
5
3
|
declare function replaceAsymmetricMatcher(actual: any, expected: any, actualReplaced?: WeakSet<object>, expectedReplaced?: WeakSet<object>): {
|
6
4
|
replacedActual: any;
|
7
5
|
replacedExpected: any;
|
package/dist/error.js
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
import {
|
1
|
+
import { diff } from './diff.js';
|
2
2
|
import { f as format, s as stringify } from './chunk-display.js';
|
3
|
-
import {
|
4
|
-
import './chunk-colors.js';
|
5
|
-
import 'concordance';
|
3
|
+
import { getOwnProperties, getType } from './helpers.js';
|
6
4
|
import 'pretty-format';
|
5
|
+
import 'diff-sequences';
|
6
|
+
import './chunk-colors.js';
|
7
7
|
import 'util';
|
8
8
|
import 'loupe';
|
9
9
|
|
@@ -73,18 +73,15 @@ function serializeError(val, seen = /* @__PURE__ */ new WeakMap()) {
|
|
73
73
|
function normalizeErrorMessage(message) {
|
74
74
|
return message.replace(/__vite_ssr_import_\d+__\./g, "");
|
75
75
|
}
|
76
|
-
function processError(err
|
76
|
+
function processError(err) {
|
77
77
|
if (!err || typeof err !== "object")
|
78
78
|
return { message: err };
|
79
79
|
if (err.stack)
|
80
80
|
err.stackStr = String(err.stack);
|
81
81
|
if (err.name)
|
82
82
|
err.nameStr = String(err.name);
|
83
|
-
const clonedActual = deepClone(err.actual, { forceWritable: true });
|
84
|
-
const clonedExpected = deepClone(err.expected, { forceWritable: true });
|
85
|
-
const { replacedActual, replacedExpected } = replaceAsymmetricMatcher(clonedActual, clonedExpected);
|
86
83
|
if (err.showDiff || err.showDiff === void 0 && err.expected !== void 0 && err.actual !== void 0)
|
87
|
-
err.diff =
|
84
|
+
err.diff = diff(err.actual, err.expected);
|
88
85
|
if (typeof err.expected !== "string")
|
89
86
|
err.expected = stringify(err.expected, 10);
|
90
87
|
if (typeof err.actual !== "string")
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@vitest/utils",
|
3
3
|
"type": "module",
|
4
|
-
"version": "0.32.
|
4
|
+
"version": "0.32.2",
|
5
5
|
"description": "Shared Vitest utility functions",
|
6
6
|
"license": "MIT",
|
7
7
|
"funding": "https://opencollective.com/vitest",
|
@@ -42,7 +42,7 @@
|
|
42
42
|
"*.d.ts"
|
43
43
|
],
|
44
44
|
"dependencies": {
|
45
|
-
"
|
45
|
+
"diff-sequences": "^29.4.3",
|
46
46
|
"loupe": "^2.3.6",
|
47
47
|
"pretty-format": "^27.5.1"
|
48
48
|
},
|