MCPcopy Create free account
hub / github.com/zxlie/FeHelper / compareTextByLine

Function compareTextByLine

apps/json-diff/diff-utils.js:9–63  ·  view source on GitHub ↗
(leftText, rightText)

Source from the content-addressed store, hash-verified

7}
8
9export function compareTextByLine(leftText, rightText) {
10 const leftLines = splitLines(leftText);
11 const rightLines = splitLines(rightText);
12 const dp = Array.from({ length: leftLines.length + 1 }, () => new Array(rightLines.length + 1).fill(0));
13
14 for (let i = leftLines.length - 1; i >= 0; i--) {
15 for (let j = rightLines.length - 1; j >= 0; j--) {
16 if (leftLines[i] === rightLines[j]) {
17 dp[i][j] = dp[i + 1][j + 1] + 1;
18 } else {
19 dp[i][j] = Math.max(dp[i + 1][j], dp[i][j + 1]);
20 }
21 }
22 }
23
24 const changedLeftLines = [];
25 const changedRightLines = [];
26 let i = 0;
27 let j = 0;
28
29 while (i < leftLines.length && j < rightLines.length) {
30 if (leftLines[i] === rightLines[j]) {
31 i++;
32 j++;
33 continue;
34 }
35
36 if (dp[i + 1][j] >= dp[i][j + 1]) {
37 changedLeftLines.push(i);
38 i++;
39 } else {
40 changedRightLines.push(j);
41 j++;
42 }
43 }
44
45 while (i < leftLines.length) {
46 changedLeftLines.push(i);
47 i++;
48 }
49
50 while (j < rightLines.length) {
51 changedRightLines.push(j);
52 j++;
53 }
54
55 return {
56 leftLines,
57 rightLines,
58 changedLeftLines,
59 changedRightLines,
60 changeCount: Math.max(changedLeftLines.length, changedRightLines.length),
61 isDifferent: changedLeftLines.length > 0 || changedRightLines.length > 0,
62 };
63}
64
65if (typeof window !== 'undefined') {
66 window.JsonDiffUtils = {

Callers 1

Calls 1

splitLinesFunction · 0.85

Tested by

no test coverage detected