-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtestDiff.js
More file actions
67 lines (62 loc) · 1.89 KB
/
testDiff.js
File metadata and controls
67 lines (62 loc) · 1.89 KB
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
const { diffChars } = require("diff");
/**
* Calculate the corrections needed to transform `original` into `corrected`.
* @param {string} original - The original text.
* @param {string} corrected - The corrected text.
* @returns {Array<{start: number, end: number, toInsert: string}>} - List of corrections.
*/
function calculateCorrections(original, corrected) {
const diffs = diffChars(original, corrected);
const corrections = [];
let currentIndex = 0;
let pendingRemoval = null;
for (const diff of diffs) {
if (diff.added) {
if (pendingRemoval) {
// Merge removal and addition into a single edit
corrections.push({
start: pendingRemoval.start,
end: pendingRemoval.end,
toInsert: diff.value,
});
pendingRemoval = null;
} else {
corrections.push({
start: currentIndex,
end: currentIndex,
toInsert: diff.value,
});
}
} else if (diff.removed) {
const length = diff.value.length;
if (pendingRemoval) {
// Extend the pending removal
pendingRemoval.end += length;
} else {
pendingRemoval = {
start: currentIndex,
end: currentIndex + length,
toInsert: "",
};
}
currentIndex += length;
} else {
if (pendingRemoval) {
// Commit any pending removal before moving on
corrections.push(pendingRemoval);
pendingRemoval = null;
}
currentIndex += diff.value.length;
}
}
// Commit any remaining pending removal
if (pendingRemoval) {
corrections.push(pendingRemoval);
}
return corrections;
}
// Example usage:
const original = "The quick brown fox jumps over the lazy dog.";
const corrected = "The very quick brown cat leaps over the super lazy dog.";
const corrections = calculateCorrections(original, corrected);
console.log(corrections);