@kerebron/test-utils 0.4.28 → 0.4.29

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.
Files changed (109) hide show
  1. package/esm/_dnt.shims.js +1 -0
  2. package/esm/_dnt.shims.js.map +1 -0
  3. package/esm/deps/jsr.io/@std/assert/1.0.16/almost_equals.js +1 -0
  4. package/esm/deps/jsr.io/@std/assert/1.0.16/almost_equals.js.map +1 -0
  5. package/esm/deps/jsr.io/@std/assert/1.0.16/array_includes.js +1 -0
  6. package/esm/deps/jsr.io/@std/assert/1.0.16/array_includes.js.map +1 -0
  7. package/esm/deps/jsr.io/@std/assert/1.0.16/assert.js +1 -0
  8. package/esm/deps/jsr.io/@std/assert/1.0.16/assert.js.map +1 -0
  9. package/esm/deps/jsr.io/@std/assert/1.0.16/assertion_error.js +1 -0
  10. package/esm/deps/jsr.io/@std/assert/1.0.16/assertion_error.js.map +1 -0
  11. package/esm/deps/jsr.io/@std/assert/1.0.16/equal.js +1 -0
  12. package/esm/deps/jsr.io/@std/assert/1.0.16/equal.js.map +1 -0
  13. package/esm/deps/jsr.io/@std/assert/1.0.16/equals.js +1 -0
  14. package/esm/deps/jsr.io/@std/assert/1.0.16/equals.js.map +1 -0
  15. package/esm/deps/jsr.io/@std/assert/1.0.16/exists.js +1 -0
  16. package/esm/deps/jsr.io/@std/assert/1.0.16/exists.js.map +1 -0
  17. package/esm/deps/jsr.io/@std/assert/1.0.16/fail.js +1 -0
  18. package/esm/deps/jsr.io/@std/assert/1.0.16/fail.js.map +1 -0
  19. package/esm/deps/jsr.io/@std/assert/1.0.16/false.js +1 -0
  20. package/esm/deps/jsr.io/@std/assert/1.0.16/false.js.map +1 -0
  21. package/esm/deps/jsr.io/@std/assert/1.0.16/greater.js +1 -0
  22. package/esm/deps/jsr.io/@std/assert/1.0.16/greater.js.map +1 -0
  23. package/esm/deps/jsr.io/@std/assert/1.0.16/greater_or_equal.js +1 -0
  24. package/esm/deps/jsr.io/@std/assert/1.0.16/greater_or_equal.js.map +1 -0
  25. package/esm/deps/jsr.io/@std/assert/1.0.16/instance_of.js +1 -0
  26. package/esm/deps/jsr.io/@std/assert/1.0.16/instance_of.js.map +1 -0
  27. package/esm/deps/jsr.io/@std/assert/1.0.16/is_error.js +1 -0
  28. package/esm/deps/jsr.io/@std/assert/1.0.16/is_error.js.map +1 -0
  29. package/esm/deps/jsr.io/@std/assert/1.0.16/less.js +1 -0
  30. package/esm/deps/jsr.io/@std/assert/1.0.16/less.js.map +1 -0
  31. package/esm/deps/jsr.io/@std/assert/1.0.16/less_or_equal.js +1 -0
  32. package/esm/deps/jsr.io/@std/assert/1.0.16/less_or_equal.js.map +1 -0
  33. package/esm/deps/jsr.io/@std/assert/1.0.16/match.js +1 -0
  34. package/esm/deps/jsr.io/@std/assert/1.0.16/match.js.map +1 -0
  35. package/esm/deps/jsr.io/@std/assert/1.0.16/mod.js +1 -0
  36. package/esm/deps/jsr.io/@std/assert/1.0.16/mod.js.map +1 -0
  37. package/esm/deps/jsr.io/@std/assert/1.0.16/not_equals.js +1 -0
  38. package/esm/deps/jsr.io/@std/assert/1.0.16/not_equals.js.map +1 -0
  39. package/esm/deps/jsr.io/@std/assert/1.0.16/not_instance_of.js +1 -0
  40. package/esm/deps/jsr.io/@std/assert/1.0.16/not_instance_of.js.map +1 -0
  41. package/esm/deps/jsr.io/@std/assert/1.0.16/not_match.js +1 -0
  42. package/esm/deps/jsr.io/@std/assert/1.0.16/not_match.js.map +1 -0
  43. package/esm/deps/jsr.io/@std/assert/1.0.16/not_strict_equals.js +1 -0
  44. package/esm/deps/jsr.io/@std/assert/1.0.16/not_strict_equals.js.map +1 -0
  45. package/esm/deps/jsr.io/@std/assert/1.0.16/object_match.js +1 -0
  46. package/esm/deps/jsr.io/@std/assert/1.0.16/object_match.js.map +1 -0
  47. package/esm/deps/jsr.io/@std/assert/1.0.16/rejects.js +1 -0
  48. package/esm/deps/jsr.io/@std/assert/1.0.16/rejects.js.map +1 -0
  49. package/esm/deps/jsr.io/@std/assert/1.0.16/strict_equals.js +1 -0
  50. package/esm/deps/jsr.io/@std/assert/1.0.16/strict_equals.js.map +1 -0
  51. package/esm/deps/jsr.io/@std/assert/1.0.16/string_includes.js +1 -0
  52. package/esm/deps/jsr.io/@std/assert/1.0.16/string_includes.js.map +1 -0
  53. package/esm/deps/jsr.io/@std/assert/1.0.16/throws.js +1 -0
  54. package/esm/deps/jsr.io/@std/assert/1.0.16/throws.js.map +1 -0
  55. package/esm/deps/jsr.io/@std/assert/1.0.16/unimplemented.js +1 -0
  56. package/esm/deps/jsr.io/@std/assert/1.0.16/unimplemented.js.map +1 -0
  57. package/esm/deps/jsr.io/@std/assert/1.0.16/unreachable.js +1 -0
  58. package/esm/deps/jsr.io/@std/assert/1.0.16/unreachable.js.map +1 -0
  59. package/esm/deps/jsr.io/@std/internal/1.0.12/build_message.js +1 -0
  60. package/esm/deps/jsr.io/@std/internal/1.0.12/build_message.js.map +1 -0
  61. package/esm/deps/jsr.io/@std/internal/1.0.12/diff.js +1 -0
  62. package/esm/deps/jsr.io/@std/internal/1.0.12/diff.js.map +1 -0
  63. package/esm/deps/jsr.io/@std/internal/1.0.12/diff_str.js +1 -0
  64. package/esm/deps/jsr.io/@std/internal/1.0.12/diff_str.js.map +1 -0
  65. package/esm/deps/jsr.io/@std/internal/1.0.12/format.js +1 -0
  66. package/esm/deps/jsr.io/@std/internal/1.0.12/format.js.map +1 -0
  67. package/esm/deps/jsr.io/@std/internal/1.0.12/styles.js +1 -0
  68. package/esm/deps/jsr.io/@std/internal/1.0.12/styles.js.map +1 -0
  69. package/esm/deps/jsr.io/@std/internal/1.0.12/types.js +1 -0
  70. package/esm/deps/jsr.io/@std/internal/1.0.12/types.js.map +1 -0
  71. package/esm/test-utils.js +1 -0
  72. package/esm/test-utils.js.map +1 -0
  73. package/package.json +5 -1
  74. package/src/_dnt.shims.ts +60 -0
  75. package/src/deps/jsr.io/@std/assert/1.0.16/almost_equals.ts +55 -0
  76. package/src/deps/jsr.io/@std/assert/1.0.16/array_includes.ts +57 -0
  77. package/src/deps/jsr.io/@std/assert/1.0.16/assert.ts +23 -0
  78. package/src/deps/jsr.io/@std/assert/1.0.16/assertion_error.ts +31 -0
  79. package/src/deps/jsr.io/@std/assert/1.0.16/equal.ts +218 -0
  80. package/src/deps/jsr.io/@std/assert/1.0.16/equals.ts +66 -0
  81. package/src/deps/jsr.io/@std/assert/1.0.16/exists.ts +31 -0
  82. package/src/deps/jsr.io/@std/assert/1.0.16/fail.ts +21 -0
  83. package/src/deps/jsr.io/@std/assert/1.0.16/false.ts +26 -0
  84. package/src/deps/jsr.io/@std/assert/1.0.16/greater.ts +30 -0
  85. package/src/deps/jsr.io/@std/assert/1.0.16/greater_or_equal.ts +36 -0
  86. package/src/deps/jsr.io/@std/assert/1.0.16/instance_of.ts +64 -0
  87. package/src/deps/jsr.io/@std/assert/1.0.16/is_error.ts +65 -0
  88. package/src/deps/jsr.io/@std/assert/1.0.16/less.ts +29 -0
  89. package/src/deps/jsr.io/@std/assert/1.0.16/less_or_equal.ts +36 -0
  90. package/src/deps/jsr.io/@std/assert/1.0.16/match.ts +30 -0
  91. package/src/deps/jsr.io/@std/assert/1.0.16/mod.ts +47 -0
  92. package/src/deps/jsr.io/@std/assert/1.0.16/not_equals.ts +37 -0
  93. package/src/deps/jsr.io/@std/assert/1.0.16/not_instance_of.ts +33 -0
  94. package/src/deps/jsr.io/@std/assert/1.0.16/not_match.ts +30 -0
  95. package/src/deps/jsr.io/@std/assert/1.0.16/not_strict_equals.ts +42 -0
  96. package/src/deps/jsr.io/@std/assert/1.0.16/object_match.ts +214 -0
  97. package/src/deps/jsr.io/@std/assert/1.0.16/rejects.ts +123 -0
  98. package/src/deps/jsr.io/@std/assert/1.0.16/strict_equals.ts +68 -0
  99. package/src/deps/jsr.io/@std/assert/1.0.16/string_includes.ts +30 -0
  100. package/src/deps/jsr.io/@std/assert/1.0.16/throws.ts +111 -0
  101. package/src/deps/jsr.io/@std/assert/1.0.16/unimplemented.ts +21 -0
  102. package/src/deps/jsr.io/@std/assert/1.0.16/unreachable.ts +21 -0
  103. package/src/deps/jsr.io/@std/internal/1.0.12/build_message.ts +147 -0
  104. package/src/deps/jsr.io/@std/internal/1.0.12/diff.ts +317 -0
  105. package/src/deps/jsr.io/@std/internal/1.0.12/diff_str.ts +208 -0
  106. package/src/deps/jsr.io/@std/internal/1.0.12/format.ts +89 -0
  107. package/src/deps/jsr.io/@std/internal/1.0.12/styles.ts +233 -0
  108. package/src/deps/jsr.io/@std/internal/1.0.12/types.ts +30 -0
  109. package/src/test-utils.ts +12 -0
@@ -0,0 +1,317 @@
1
+ // Copyright 2018-2025 the Deno authors. MIT license.
2
+ // This module is browser compatible.
3
+
4
+ import type { DiffResult, DiffType } from "./types.js";
5
+
6
+ /** Represents the farthest point in the diff algorithm. */
7
+ export interface FarthestPoint {
8
+ /** The y-coordinate of the point. */
9
+ y: number;
10
+ /** The id of the point. */
11
+ id: number;
12
+ }
13
+
14
+ const REMOVED = 1;
15
+ const COMMON = 2;
16
+ const ADDED = 3;
17
+
18
+ /**
19
+ * Creates an array of common elements between two arrays.
20
+ *
21
+ * @typeParam T The type of elements in the arrays.
22
+ *
23
+ * @param A The first array.
24
+ * @param B The second array.
25
+ *
26
+ * @returns An array containing the common elements between the two arrays.
27
+ *
28
+ * @example Usage
29
+ * ```ts
30
+ * import { createCommon } from "@std/internal/diff";
31
+ * import { assertEquals } from "@std/assert";
32
+ *
33
+ * const a = [1, 2, 3];
34
+ * const b = [1, 2, 4];
35
+ *
36
+ * assertEquals(createCommon(a, b), [1, 2]);
37
+ * ```
38
+ */
39
+ export function createCommon<T>(A: T[], B: T[]): T[] {
40
+ const common: T[] = [];
41
+ if (A.length === 0 || B.length === 0) return [];
42
+ for (let i = 0; i < Math.min(A.length, B.length); i += 1) {
43
+ const a = A[i];
44
+ const b = B[i];
45
+ if (a !== undefined && a === b) {
46
+ common.push(a);
47
+ } else {
48
+ return common;
49
+ }
50
+ }
51
+ return common;
52
+ }
53
+
54
+ /**
55
+ * Asserts that the value is a {@linkcode FarthestPoint}.
56
+ * If not, an error is thrown.
57
+ *
58
+ * @param value The value to check.
59
+ *
60
+ * @returns A void value that returns once the assertion completes.
61
+ *
62
+ * @example Usage
63
+ * ```ts
64
+ * import { assertFp } from "@std/internal/diff";
65
+ * import { assertThrows } from "@std/assert";
66
+ *
67
+ * assertFp({ y: 0, id: 0 });
68
+ * assertThrows(() => assertFp({ id: 0 }));
69
+ * assertThrows(() => assertFp({ y: 0 }));
70
+ * assertThrows(() => assertFp(undefined));
71
+ * ```
72
+ */
73
+ export function assertFp(value: unknown): asserts value is FarthestPoint {
74
+ if (
75
+ value == null ||
76
+ typeof value !== "object" ||
77
+ typeof (value as FarthestPoint)?.y !== "number" ||
78
+ typeof (value as FarthestPoint)?.id !== "number"
79
+ ) {
80
+ throw new Error(
81
+ `Unexpected value, expected 'FarthestPoint': received ${typeof value}`,
82
+ );
83
+ }
84
+ }
85
+
86
+ /**
87
+ * Creates an array of backtraced differences.
88
+ *
89
+ * @typeParam T The type of elements in the arrays.
90
+ *
91
+ * @param A The first array.
92
+ * @param B The second array.
93
+ * @param current The current {@linkcode FarthestPoint}.
94
+ * @param swapped Boolean indicating if the arrays are swapped.
95
+ * @param routes The routes array.
96
+ * @param diffTypesPtrOffset The offset of the diff types in the routes array.
97
+ *
98
+ * @returns An array of backtraced differences.
99
+ *
100
+ * @example Usage
101
+ * ```ts
102
+ * import { backTrace } from "@std/internal/diff";
103
+ * import { assertEquals } from "@std/assert";
104
+ *
105
+ * assertEquals(
106
+ * backTrace([], [], { y: 0, id: 0 }, false, new Uint32Array(0), 0),
107
+ * [],
108
+ * );
109
+ * ```
110
+ */
111
+ export function backTrace<T>(
112
+ A: T[],
113
+ B: T[],
114
+ current: FarthestPoint,
115
+ swapped: boolean,
116
+ routes: Uint32Array,
117
+ diffTypesPtrOffset: number,
118
+ ): Array<{
119
+ type: DiffType;
120
+ value: T;
121
+ }> {
122
+ const M = A.length;
123
+ const N = B.length;
124
+ const result: { type: DiffType; value: T }[] = [];
125
+ let a = M - 1;
126
+ let b = N - 1;
127
+ let j = routes[current.id];
128
+ let type = routes[current.id + diffTypesPtrOffset];
129
+ while (true) {
130
+ if (!j && !type) break;
131
+ const prev = j!;
132
+ if (type === REMOVED) {
133
+ result.unshift({
134
+ type: swapped ? "removed" : "added",
135
+ value: B[b]!,
136
+ });
137
+ b -= 1;
138
+ } else if (type === ADDED) {
139
+ result.unshift({
140
+ type: swapped ? "added" : "removed",
141
+ value: A[a]!,
142
+ });
143
+ a -= 1;
144
+ } else {
145
+ result.unshift({ type: "common", value: A[a]! });
146
+ a -= 1;
147
+ b -= 1;
148
+ }
149
+ j = routes[prev];
150
+ type = routes[prev + diffTypesPtrOffset];
151
+ }
152
+ return result;
153
+ }
154
+
155
+ /**
156
+ * Creates a {@linkcode FarthestPoint}.
157
+ *
158
+ * @param k The current index.
159
+ * @param M The length of the first array.
160
+ * @param routes The routes array.
161
+ * @param diffTypesPtrOffset The offset of the diff types in the routes array.
162
+ * @param ptr The current pointer.
163
+ * @param slide The slide {@linkcode FarthestPoint}.
164
+ * @param down The down {@linkcode FarthestPoint}.
165
+ *
166
+ * @returns A {@linkcode FarthestPoint}.
167
+ *
168
+ * @example Usage
169
+ * ```ts
170
+ * import { createFp } from "@std/internal/diff";
171
+ * import { assertEquals } from "@std/assert";
172
+ *
173
+ * assertEquals(
174
+ * createFp(
175
+ * 0,
176
+ * 0,
177
+ * new Uint32Array(0),
178
+ * 0,
179
+ * 0,
180
+ * { y: -1, id: 0 },
181
+ * { y: 0, id: 0 },
182
+ * ),
183
+ * { y: -1, id: 1 },
184
+ * );
185
+ * ```
186
+ */
187
+ export function createFp(
188
+ k: number,
189
+ M: number,
190
+ routes: Uint32Array,
191
+ diffTypesPtrOffset: number,
192
+ ptr: number,
193
+ slide?: FarthestPoint,
194
+ down?: FarthestPoint,
195
+ ): FarthestPoint {
196
+ if (slide && slide.y === -1 && down && down.y === -1) {
197
+ return { y: 0, id: 0 };
198
+ }
199
+ const isAdding = (down?.y === -1) ||
200
+ k === M ||
201
+ (slide?.y ?? 0) > (down?.y ?? 0) + 1;
202
+ if (slide && isAdding) {
203
+ const prev = slide.id;
204
+ ptr++;
205
+ routes[ptr] = prev;
206
+ routes[ptr + diffTypesPtrOffset] = ADDED;
207
+ return { y: slide.y, id: ptr };
208
+ }
209
+ if (down && !isAdding) {
210
+ const prev = down.id;
211
+ ptr++;
212
+ routes[ptr] = prev;
213
+ routes[ptr + diffTypesPtrOffset] = REMOVED;
214
+ return { y: down.y + 1, id: ptr };
215
+ }
216
+ throw new Error("Unexpected missing FarthestPoint");
217
+ }
218
+
219
+ /**
220
+ * Renders the differences between the actual and expected values.
221
+ *
222
+ * @typeParam T The type of elements in the arrays.
223
+ *
224
+ * @param A Actual value
225
+ * @param B Expected value
226
+ *
227
+ * @returns An array of differences between the actual and expected values.
228
+ *
229
+ * @example Usage
230
+ * ```ts
231
+ * import { diff } from "@std/internal/diff";
232
+ * import { assertEquals } from "@std/assert";
233
+ *
234
+ * const a = [1, 2, 3];
235
+ * const b = [1, 2, 4];
236
+ *
237
+ * assertEquals(diff(a, b), [
238
+ * { type: "common", value: 1 },
239
+ * { type: "common", value: 2 },
240
+ * { type: "removed", value: 3 },
241
+ * { type: "added", value: 4 },
242
+ * ]);
243
+ * ```
244
+ */
245
+ export function diff<T>(A: T[], B: T[]): DiffResult<T>[] {
246
+ const prefixCommon = createCommon(A, B);
247
+ A = A.slice(prefixCommon.length);
248
+ B = B.slice(prefixCommon.length);
249
+ const swapped = B.length > A.length;
250
+ [A, B] = swapped ? [B, A] : [A, B];
251
+ const M = A.length;
252
+ const N = B.length;
253
+ if (!M && !N && !prefixCommon.length) return [];
254
+ if (!N) {
255
+ return [
256
+ ...prefixCommon.map((value) => ({ type: "common", value })),
257
+ ...A.map((value) => ({ type: swapped ? "added" : "removed", value })),
258
+ ] as DiffResult<T>[];
259
+ }
260
+ const offset = N;
261
+ const delta = M - N;
262
+ const length = M + N + 1;
263
+ const fp: FarthestPoint[] = Array.from({ length }, () => ({ y: -1, id: -1 }));
264
+
265
+ /**
266
+ * Note: this buffer is used to save memory and improve performance. The first
267
+ * half is used to save route and the last half is used to save diff type.
268
+ */
269
+ const routes = new Uint32Array((M * N + length + 1) * 2);
270
+ const diffTypesPtrOffset = routes.length / 2;
271
+ let ptr = 0;
272
+
273
+ function snake<T>(
274
+ k: number,
275
+ A: T[],
276
+ B: T[],
277
+ slide?: FarthestPoint,
278
+ down?: FarthestPoint,
279
+ ): FarthestPoint {
280
+ const M = A.length;
281
+ const N = B.length;
282
+ const fp = createFp(k, M, routes, diffTypesPtrOffset, ptr, slide, down);
283
+ ptr = fp.id;
284
+ while (fp.y + k < M && fp.y < N && A[fp.y + k] === B[fp.y]) {
285
+ const prev = fp.id;
286
+ ptr++;
287
+ fp.id = ptr;
288
+ fp.y += 1;
289
+ routes[ptr] = prev;
290
+ routes[ptr + diffTypesPtrOffset] = COMMON;
291
+ }
292
+ return fp;
293
+ }
294
+
295
+ let currentFp = fp[delta + offset];
296
+ assertFp(currentFp);
297
+ let p = -1;
298
+ while (currentFp.y < N) {
299
+ p = p + 1;
300
+ for (let k = -p; k < delta; ++k) {
301
+ const index = k + offset;
302
+ fp[index] = snake(k, A, B, fp[index - 1], fp[index + 1]);
303
+ }
304
+ for (let k = delta + p; k > delta; --k) {
305
+ const index = k + offset;
306
+ fp[index] = snake(k, A, B, fp[index - 1], fp[index + 1]);
307
+ }
308
+ const index = delta + offset;
309
+ fp[delta + offset] = snake(delta, A, B, fp[index - 1], fp[index + 1]);
310
+ currentFp = fp[delta + offset];
311
+ assertFp(currentFp);
312
+ }
313
+ return [
314
+ ...prefixCommon.map((value) => ({ type: "common", value })),
315
+ ...backTrace(A, B, currentFp, swapped, routes, diffTypesPtrOffset),
316
+ ] as DiffResult<T>[];
317
+ }
@@ -0,0 +1,208 @@
1
+ // Copyright 2018-2025 the Deno authors. MIT license.
2
+ // This module is browser compatible.
3
+
4
+ import type { ChangedDiffResult, DiffResult } from "./types.js";
5
+ import { diff } from "./diff.js";
6
+
7
+ /**
8
+ * Unescape invisible characters.
9
+ *
10
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#escape_sequences}
11
+ *
12
+ * @param string String to unescape.
13
+ *
14
+ * @returns Unescaped string.
15
+ *
16
+ * @example Usage
17
+ * ```ts
18
+ * import { unescape } from "@std/internal/diff-str";
19
+ * import { assertEquals } from "@std/assert";
20
+ *
21
+ * assertEquals(unescape("Hello\nWorld"), "Hello\\n\nWorld");
22
+ * ```
23
+ */
24
+ export function unescape(string: string): string {
25
+ return string
26
+ .replaceAll("\\", "\\\\")
27
+ .replaceAll("\b", "\\b")
28
+ .replaceAll("\f", "\\f")
29
+ .replaceAll("\t", "\\t")
30
+ .replaceAll("\v", "\\v")
31
+ // This does not remove line breaks
32
+ .replaceAll(
33
+ /\r\n|\r|\n/g,
34
+ (str) => str === "\r" ? "\\r" : str === "\n" ? "\\n\n" : "\\r\\n\r\n",
35
+ );
36
+ }
37
+
38
+ const WHITESPACE_SYMBOLS =
39
+ /((?:\\[bftv]|[^\S\r\n])+|\\[rn\\]|[()[\]{}'"\r\n]|\b)/;
40
+
41
+ /**
42
+ * Tokenizes a string into an array of tokens.
43
+ *
44
+ * @param string The string to tokenize.
45
+ * @param wordDiff If true, performs word-based tokenization. Default is false.
46
+ *
47
+ * @returns An array of tokens.
48
+ *
49
+ * @example Usage
50
+ * ```ts
51
+ * import { tokenize } from "@std/internal/diff-str";
52
+ * import { assertEquals } from "@std/assert";
53
+ *
54
+ * assertEquals(tokenize("Hello\nWorld"), ["Hello\n", "World"]);
55
+ * ```
56
+ */
57
+ export function tokenize(string: string, wordDiff = false): string[] {
58
+ if (wordDiff) {
59
+ return string
60
+ .split(WHITESPACE_SYMBOLS)
61
+ .filter((token) => token);
62
+ }
63
+ const tokens: string[] = [];
64
+ const lines = string.split(/(\n|\r\n)/).filter((line) => line);
65
+
66
+ for (const [i, line] of lines.entries()) {
67
+ if (i % 2) {
68
+ tokens[tokens.length - 1] += line;
69
+ } else {
70
+ tokens.push(line);
71
+ }
72
+ }
73
+ return tokens;
74
+ }
75
+
76
+ /**
77
+ * Create details by filtering relevant word-diff for current line and merge
78
+ * "space-diff" if surrounded by word-diff for cleaner displays.
79
+ *
80
+ * @param line Current line
81
+ * @param tokens Word-diff tokens
82
+ *
83
+ * @returns Array of diff results.
84
+ *
85
+ * @example Usage
86
+ * ```ts
87
+ * import { createDetails } from "@std/internal/diff-str";
88
+ * import { assertEquals } from "@std/assert";
89
+ *
90
+ * const tokens = [
91
+ * { type: "added", value: "a" },
92
+ * { type: "removed", value: "b" },
93
+ * { type: "common", value: "c" },
94
+ * ] as const;
95
+ * assertEquals(
96
+ * createDetails({ type: "added", value: "a" }, [...tokens]),
97
+ * [{ type: "added", value: "a" }, { type: "common", value: "c" }]
98
+ * );
99
+ * ```
100
+ */
101
+ export function createDetails(
102
+ line: DiffResult<string>,
103
+ tokens: DiffResult<string>[],
104
+ ): DiffResult<string>[] {
105
+ return tokens.filter(({ type }) => type === line.type || type === "common")
106
+ .map((result, i, t) => {
107
+ const token = t[i - 1];
108
+ if (
109
+ (result.type === "common") && token &&
110
+ (token.type === t[i + 1]?.type) && /\s+/.test(result.value)
111
+ ) {
112
+ return {
113
+ ...result,
114
+ type: token.type,
115
+ };
116
+ }
117
+ return result;
118
+ });
119
+ }
120
+
121
+ const NON_WHITESPACE_REGEXP = /\S/;
122
+
123
+ /**
124
+ * Renders the differences between the actual and expected strings. Partially
125
+ * inspired from {@link https://github.com/kpdecker/jsdiff}.
126
+ *
127
+ * @param A Actual string
128
+ * @param B Expected string
129
+ *
130
+ * @returns Array of diff results.
131
+ *
132
+ * @example Usage
133
+ * ```ts
134
+ * import { diffStr } from "@std/internal/diff-str";
135
+ * import { assertEquals } from "@std/assert";
136
+ *
137
+ * assertEquals(diffStr("Hello!", "Hello"), [
138
+ * {
139
+ * type: "removed",
140
+ * value: "Hello!\n",
141
+ * details: [
142
+ * { type: "common", value: "Hello" },
143
+ * { type: "removed", value: "!" },
144
+ * { type: "common", value: "\n" }
145
+ * ]
146
+ * },
147
+ * {
148
+ * type: "added",
149
+ * value: "Hello\n",
150
+ * details: [
151
+ * { type: "common", value: "Hello" },
152
+ * { type: "common", value: "\n" }
153
+ * ]
154
+ * }
155
+ * ]);
156
+ * ```
157
+ */
158
+ export function diffStr(A: string, B: string): DiffResult<string>[] {
159
+ // Compute multi-line diff
160
+ const diffResult = diff(
161
+ tokenize(`${unescape(A)}\n`),
162
+ tokenize(`${unescape(B)}\n`),
163
+ );
164
+
165
+ const added = [];
166
+ const removed = [];
167
+ for (const result of diffResult) {
168
+ if (result.type === "added") {
169
+ added.push(result);
170
+ }
171
+ if (result.type === "removed") {
172
+ removed.push(result);
173
+ }
174
+ }
175
+
176
+ // Compute word-diff
177
+ const hasMoreRemovedLines = added.length < removed.length;
178
+ const aLines = hasMoreRemovedLines ? added : removed;
179
+ const bLines = hasMoreRemovedLines ? removed : added;
180
+ for (const a of aLines) {
181
+ let tokens = [] as Array<DiffResult<string>>;
182
+ let b: undefined | ChangedDiffResult<string>;
183
+ // Search another diff line with at least one common token
184
+ while (bLines.length) {
185
+ b = bLines.shift();
186
+ const tokenized = [
187
+ tokenize(a.value, true),
188
+ tokenize(b!.value, true),
189
+ ] as [string[], string[]];
190
+ if (hasMoreRemovedLines) tokenized.reverse();
191
+ tokens = diff(tokenized[0], tokenized[1]);
192
+ if (
193
+ tokens.some(({ type, value }) =>
194
+ type === "common" && NON_WHITESPACE_REGEXP.test(value)
195
+ )
196
+ ) {
197
+ break;
198
+ }
199
+ }
200
+ // Register word-diff details
201
+ a.details = createDetails(a, tokens);
202
+ if (b) {
203
+ b.details = createDetails(b, tokens);
204
+ }
205
+ }
206
+
207
+ return diffResult;
208
+ }
@@ -0,0 +1,89 @@
1
+ // Copyright 2018-2025 the Deno authors. MIT license.
2
+ // This module is browser compatible.
3
+
4
+ /** An inspect function conforming to the shape of `Deno.inspect` and `node:util`'s `inspect` */
5
+ import * as dntShim from "../../../../../_dnt.shims.js";
6
+
7
+ export type InspectFn = (
8
+ v: unknown,
9
+ options: {
10
+ depth: number;
11
+ sorted: boolean;
12
+ trailingComma: boolean;
13
+ compact: boolean;
14
+ iterableLimit: number;
15
+ getters: boolean;
16
+ strAbbreviateSize: number;
17
+ },
18
+ ) => string;
19
+
20
+ /**
21
+ * Converts the input into a string. Objects, Sets and Maps are sorted so as to
22
+ * make tests less flaky.
23
+ *
24
+ * @param v Value to be formatted
25
+ *
26
+ * @returns The formatted string
27
+ *
28
+ * @example Usage
29
+ * ```ts
30
+ * import { format } from "@std/internal/format";
31
+ * import { assertEquals } from "@std/assert";
32
+ *
33
+ * assertEquals(format({ a: 1, b: 2 }), "{\n a: 1,\n b: 2,\n}");
34
+ * assertEquals(format(new Set([1, 2])), "Set(2) {\n 1,\n 2,\n}");
35
+ * assertEquals(format(new Map([[1, 2]])), "Map(1) {\n 1 => 2,\n}");
36
+ * ```
37
+ */
38
+ export function format(v: unknown): string {
39
+ // deno-lint-ignore no-explicit-any
40
+ const { Deno, process } = dntShim.dntGlobalThis as any;
41
+
42
+ const inspect: InspectFn | undefined = Deno?.inspect ??
43
+ process?.getBuiltinModule?.("node:util")?.inspect;
44
+
45
+ return typeof inspect === "function"
46
+ ? inspect(v, {
47
+ depth: Infinity,
48
+ sorted: true,
49
+ trailingComma: true,
50
+ compact: false,
51
+ iterableLimit: Infinity,
52
+ // getters should be true in assertEquals.
53
+ getters: true,
54
+ strAbbreviateSize: Infinity,
55
+ })
56
+ : basicInspect(v);
57
+ }
58
+
59
+ const formatters: ((v: unknown) => string | undefined)[] = [
60
+ (v) => {
61
+ if (typeof v === "undefined") return "undefined";
62
+ if (typeof v === "bigint") return `${v}n`;
63
+
64
+ if (
65
+ typeof v === "string" ||
66
+ typeof v === "number" ||
67
+ typeof v === "boolean" ||
68
+ v === null ||
69
+ Array.isArray(v) ||
70
+ [null, Object.prototype].includes(Object.getPrototypeOf(v))
71
+ ) {
72
+ return JSON.stringify(v, null, 2);
73
+ }
74
+ },
75
+ (v) => String(v),
76
+ (v) => Object.prototype.toString.call(v),
77
+ ];
78
+
79
+ // for environments lacking both `Deno.inspect` and `process.inspect`
80
+ function basicInspect(v: unknown): string {
81
+ for (const fmt of formatters) {
82
+ try {
83
+ const result = fmt(v);
84
+ if (typeof result === "string") return result;
85
+ } catch { /* try the next one */ }
86
+ }
87
+
88
+ return "[[Unable to format value]]";
89
+ }