1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208 |
 
 
 
 
x1256
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x1256
x1297
x1297
x1297
x1297
x1297
x1297
 
x1297
x1297
x1297
 
x1297
 
x1256
x1256
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x1256
x1334
x1443
x1443
x1443
x1443
x1439
x1439
 
x1334
x1444
x1587
x1535
x1593
x1593
x1444
x1371
x1334
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x1256
x1256
x1256
 
x1296
x1296
x1433
x1433
x1433
x1433
x1433
x1441
x1441
x1441
x1441
x1441
x1562
x1296
x1296
 
x1256
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x1256
 
x1272
x1272
x1272
 
 
x1272
x1272
x1272
x1316
x1336
x1336
x1316
x1335
x1335
x1316
 
 
x1272
x1272
x1272
x1272
x1290
x1290
 
x1290
x1309
x1309
x1309
x1309
x1309
x1309
x1309
x1309
x1309
x1309
 
x1309
x1322
x1322
x1309
 
x1290
x1290
x1290
x1290
x1290
 
x1272
x1272 |
|
// Copyright 2018-2025 the Deno authors. MIT license.
// This module is browser compatible.
import type { DiffResult } from "./types.ts";
import { diff } from "./diff.ts";
/**
* Unescape invisible characters.
*
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#escape_sequences}
*
* @param string String to unescape.
*
* @returns Unescaped string.
*
* @example Usage
* ```ts
* import { unescape } from "@std/internal/diff-str";
* import { assertEquals } from "@std/assert";
*
* assertEquals(unescape("Hello\nWorld"), "Hello\\n\nWorld");
* ```
*/
export function unescape(string: string): string {
return string
.replaceAll("\\", "\\\\")
.replaceAll("\b", "\\b")
.replaceAll("\f", "\\f")
.replaceAll("\t", "\\t")
.replaceAll("\v", "\\v")
// This does not remove line breaks
.replaceAll(
/\r\n|\r|\n/g,
(str) => str === "\r" ? "\\r" : str === "\n" ? "\\n\n" : "\\r\\n\r\n",
);
}
const WHITESPACE_SYMBOLS =
/((?:\\[bftv]|[^\S\r\n])+|\\[rn\\]|[()[\]{}'"\r\n]|\b)/;
/**
* Tokenizes a string into an array of tokens.
*
* @param string The string to tokenize.
* @param wordDiff If true, performs word-based tokenization. Default is false.
*
* @returns An array of tokens.
*
* @example Usage
* ```ts
* import { tokenize } from "@std/internal/diff-str";
* import { assertEquals } from "@std/assert";
*
* assertEquals(tokenize("Hello\nWorld"), ["Hello\n", "World"]);
* ```
*/
export function tokenize(string: string, wordDiff = false): string[] {
if (wordDiff) {
return string
.split(WHITESPACE_SYMBOLS)
.filter((token) => token);
}
const tokens: string[] = [];
const lines = string.split(/(\n|\r\n)/).filter((line) => line);
for (const [i, line] of lines.entries()) {
if (i % 2) {
tokens[tokens.length - 1] += line;
} else {
tokens.push(line);
}
}
return tokens;
}
/**
* Create details by filtering relevant word-diff for current line and merge
* "space-diff" if surrounded by word-diff for cleaner displays.
*
* @param line Current line
* @param tokens Word-diff tokens
*
* @returns Array of diff results.
*
* @example Usage
* ```ts
* import { createDetails } from "@std/internal/diff-str";
* import { assertEquals } from "@std/assert";
*
* const tokens = [
* { type: "added", value: "a" },
* { type: "removed", value: "b" },
* { type: "common", value: "c" },
* ] as const;
* assertEquals(
* createDetails({ type: "added", value: "a" }, [...tokens]),
* [{ type: "added", value: "a" }, { type: "common", value: "c" }]
* );
* ```
*/
export function createDetails(
line: DiffResult<string>,
tokens: DiffResult<string>[],
): DiffResult<string>[] {
return tokens.filter(({ type }) => type === line.type || type === "common")
.map((result, i, t) => {
const token = t[i - 1];
if (
(result.type === "common") && token &&
(token.type === t[i + 1]?.type) && /\s+/.test(result.value)
) {
return {
...result,
type: token.type,
};
}
return result;
});
}
const NON_WHITESPACE_REGEXP = /\S/;
/**
* Renders the differences between the actual and expected strings. Partially
* inspired from {@link https://github.com/kpdecker/jsdiff}.
*
* @param A Actual string
* @param B Expected string
*
* @returns Array of diff results.
*
* @example Usage
* ```ts
* import { diffStr } from "@std/internal/diff-str";
* import { assertEquals } from "@std/assert";
*
* assertEquals(diffStr("Hello!", "Hello"), [
* {
* type: "removed",
* value: "Hello!\n",
* details: [
* { type: "common", value: "Hello" },
* { type: "removed", value: "!" },
* { type: "common", value: "\n" }
* ]
* },
* {
* type: "added",
* value: "Hello\n",
* details: [
* { type: "common", value: "Hello" },
* { type: "common", value: "\n" }
* ]
* }
* ]);
* ```
*/
export function diffStr(A: string, B: string): DiffResult<string>[] {
// Compute multi-line diff
const diffResult = diff(
tokenize(`${unescape(A)}\n`),
tokenize(`${unescape(B)}\n`),
);
const added = [];
const removed = [];
for (const result of diffResult) {
if (result.type === "added") {
added.push(result);
}
if (result.type === "removed") {
removed.push(result);
}
}
// Compute word-diff
const hasMoreRemovedLines = added.length < removed.length;
const aLines = hasMoreRemovedLines ? added : removed;
const bLines = hasMoreRemovedLines ? removed : added;
for (const a of aLines) {
let tokens = [] as Array<DiffResult<string>>;
let b: undefined | DiffResult<string>;
// Search another diff line with at least one common token
while (bLines.length) {
b = bLines.shift();
const tokenized = [
tokenize(a.value, true),
tokenize(b!.value, true),
] as [string[], string[]];
if (hasMoreRemovedLines) tokenized.reverse();
tokens = diff(tokenized[0], tokenized[1]);
if (
tokens.some(({ type, value }) =>
type === "common" && NON_WHITESPACE_REGEXP.test(value)
)
) {
break;
}
}
// Register word-diff details
a.details = createDetails(a, tokens);
if (b) {
b.details = createDetails(b, tokens);
}
}
return diffResult;
}
|