gent-cli 2.0.0 → 5.0.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/src/utils/diff.js DELETED
@@ -1,121 +0,0 @@
1
- /**
2
- * Myers Diff Algorithm Implementation
3
- * Calculates the difference between two sequences of text
4
- */
5
-
6
- /**
7
- * Compare two strings and return the differences using Myers algorithm
8
- * @param {String} oldText - The original text
9
- * @param {String} newText - The new text
10
- * @returns {Array} Array of changes [{ type: 'equal'|'insert'|'delete', value: string }]
11
- */
12
- function computeDiff(oldText, newText) {
13
- // Split into lines for line-by-line diff
14
- const oldLines = oldText.split('\n');
15
- const newLines = newText.split('\n');
16
-
17
- // If both are empty or identical
18
- if (oldText === newText) {
19
- return oldLines.map(line => ({ type: 'equal', value: line }));
20
- }
21
-
22
- const path = exploreEditGraph(oldLines, newLines);
23
- return backtrack(path, oldLines, newLines);
24
- }
25
-
26
- /**
27
- * Explore the edit graph to find the shortest path (shortest edit script)
28
- * @param {Array} oldLines
29
- * @param {Array} newLines
30
- * @returns {Array} Trace of the path
31
- */
32
- function exploreEditGraph(oldLines, newLines) {
33
- const n = oldLines.length;
34
- const m = newLines.length;
35
- const max = n + m;
36
-
37
- // v array stores the x-coordinate of the furthest reaching D-path
38
- // using object/map to handle negative indices (-max to +max)
39
- const v = { 1: 0 };
40
- const trace = [];
41
-
42
- for (let d = 0; d <= max; d++) {
43
- trace.push({ ...v });
44
-
45
- for (let k = -d; k <= d; k += 2) {
46
- let x;
47
-
48
- // Choose move: down (insertion) or right (deletion)
49
- if (k === -d || (k !== d && (v[k - 1] || 0) < (v[k + 1] || 0))) {
50
- x = v[k + 1] || 0; // Move down
51
- } else {
52
- x = (v[k - 1] || 0) + 1; // Move right
53
- }
54
-
55
- let y = x - k;
56
-
57
- // Follow diagonal (equal lines)
58
- while (x < n && y < m && oldLines[x] === newLines[y]) {
59
- x++;
60
- y++;
61
- }
62
-
63
- v[k] = x;
64
-
65
- if (x >= n && y >= m) {
66
- return trace;
67
- }
68
- }
69
- }
70
- return trace;
71
- }
72
-
73
- /**
74
- * Backtrack from the end to generate the diff
75
- * @param {Array} trace
76
- * @param {Array} oldLines
77
- * @param {Array} newLines
78
- * @returns {Array}
79
- */
80
- function backtrack(trace, oldLines, newLines) {
81
- let x = oldLines.length;
82
- let y = newLines.length;
83
- const diff = [];
84
-
85
- for (let d = trace.length - 1; d >= 0; d--) {
86
- const v = trace[d];
87
- const k = x - y;
88
-
89
- let prevK;
90
- if (k === -d || (k !== d && (v[k - 1] || 0) < (v[k + 1] || 0))) {
91
- prevK = k + 1;
92
- } else {
93
- prevK = k - 1;
94
- }
95
-
96
- const prevX = v[prevK] || 0;
97
- const prevY = prevX - prevK;
98
-
99
- while (x > prevX && y > prevY) {
100
- diff.unshift({ type: 'equal', value: oldLines[x - 1] });
101
- x--;
102
- y--;
103
- }
104
-
105
- if (d > 0) {
106
- if (x === prevX) {
107
- diff.unshift({ type: 'insert', value: newLines[y - 1] });
108
- y--;
109
- } else {
110
- diff.unshift({ type: 'delete', value: oldLines[x - 1] });
111
- x--;
112
- }
113
- }
114
- }
115
-
116
- return diff;
117
- }
118
-
119
- module.exports = {
120
- computeDiff
121
- };