File size: 2,601 Bytes
4e1096a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Diff function similar to Linux `diff`
 * Usage: diff(str1, str2)
 */

export function diff(str1: string, str2: string) {
  const lines1 = str1.split('\n').filter((line) => line.trim() !== '');
  const lines2 = str2.split('\n').filter((line) => line.trim() !== '');

  const lcs = longestCommonSubsequence(lines1, lines2);

  let i = 0,
    j = 0,
    k = 0;
  const result: string[] = [];

  const addRange = (start: number, end: number) => (start === end ? `${start}` : `${start},${end}`);

  while (i < lines1.length || j < lines2.length) {
    if (k < lcs.length && i < lines1.length && trimCompare(lines1[i], lcs[k])) {
      // common line
      i++;
      j++;
      k++;
    } else {
      const delStart = i;
      const addStart = j;

      while (i < lines1.length && (k >= lcs.length || !trimCompare(lines1[i], lcs[k]))) i++;
      while (j < lines2.length && (k >= lcs.length || !trimCompare(lines2[j], lcs[k]))) j++;

      const delRange = addRange(delStart + 1, i);
      const addRangeStr = addRange(addStart + 1, j);

      if (delStart < i && addStart < j) {
        // change
        result.push(`${delRange}c${addRangeStr}`);
        for (let m = delStart; m < i; m++) result.push(`< ${lines1[m]}`);
        result.push('---');
        for (let m = addStart; m < j; m++) result.push(`> ${lines2[m]}`);
      } else if (delStart < i) {
        // deletion
        result.push(`${delRange}d${addStart}`);
        for (let m = delStart; m < i; m++) result.push(`< ${lines1[m]}`);
      } else if (addStart < j) {
        // addition
        result.push(`${delStart}a${addRangeStr}`);
        for (let m = addStart; m < j; m++) result.push(`> ${lines2[m]}`);
      }
    }
  }

  return result.join('\n');
}

function trimCompare(a: string | undefined, b: string | undefined) {
  return (a || '').trim() === (b || '').trim();
}

function longestCommonSubsequence(arr1: string[], arr2: string[]) {
  const m = arr1.length;
  const n = arr2.length;
  const dp = Array(m + 1)
    .fill(null)
    .map(() => Array(n + 1).fill(0));

  for (let i = 1; i <= m; i++) {
    for (let j = 1; j <= n; j++) {
      if (trimCompare(arr1[i - 1], arr2[j - 1])) {
        dp[i]![j] = dp[i - 1]![j - 1] + 1;
      } else {
        dp[i]![j] = Math.max(dp[i - 1]![j], dp[i]![j - 1]);
      }
    }
  }

  const lcs: string[] = [];
  let i = m,
    j = n;

  while (i > 0 && j > 0) {
    if (trimCompare(arr1[i - 1], arr2[j - 1])) {
      lcs.unshift(arr1[i - 1]!);
      i--;
      j--;
    } else if (dp[i - 1]![j] > dp[i]![j - 1]) {
      i--;
    } else {
      j--;
    }
  }

  return lcs;
}