Spaces:
Paused

File size: 1,092 Bytes
1b55371
 
 
 
 
 
2829e9b
 
 
 
 
 
1b55371
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
export interface DiffLine {
  type: 'added' | 'removed' | 'unchanged';
  value: string;
}

export function computeDiff(oldText: string, newText: string): DiffLine[] {
  // Normalize line endings to avoid ghost diffs from CRLF/LF mismatch
  const cleanOld = (oldText || '').replace(/\r\n/g, '\n').replace(/\r/g, '\n');
  const cleanNew = (newText || '').replace(/\r\n/g, '\n').replace(/\r/g, '\n');

  const oldLines = cleanOld.split('\n').map(l => l.trimEnd());
  const newLines = cleanNew.split('\n').map(l => l.trimEnd());

  const diff: DiffLine[] = [];
  let i = 0;
  let j = 0;

  while (i < oldLines.length || j < newLines.length) {
    if (i < oldLines.length && j < newLines.length && oldLines[i] === newLines[j]) {
      diff.push({ type: 'unchanged', value: oldLines[i] });
      i++;
      j++;
    } else if (j < newLines.length && (i === oldLines.length || !oldLines.slice(i).includes(newLines[j]))) {
      diff.push({ type: 'added', value: newLines[j] });
      j++;
    } else {
      diff.push({ type: 'removed', value: oldLines[i] });
      i++;
    }
  }

  return diff;
}