MCPcopy
hub / github.com/jestjs/jest / joinAlignedDiffsNoExpand

Function joinAlignedDiffsNoExpand

packages/jest-diff/src/joinAlignedDiffs.ts:112–283  ·  view source on GitHub ↗
(
  diffs: Array<Diff>,
  options: DiffOptionsNormalized,
)

Source from the content-addressed store, hash-verified

110// Given array of aligned strings with inverse highlight formatting,
111// return joined lines with diff formatting (and patch marks, if needed).
112export const joinAlignedDiffsNoExpand = (
113 diffs: Array<Diff>,
114 options: DiffOptionsNormalized,
115): string => {
116 const iLength = diffs.length;
117 const nContextLines = options.contextLines;
118 const nContextLines2 = nContextLines + nContextLines;
119
120 // First pass: count output lines and see if it has patches.
121 let jLength = iLength;
122 let hasExcessAtStartOrEnd = false;
123 let nExcessesBetweenChanges = 0;
124 let i = 0;
125 while (i !== iLength) {
126 const iStart = i;
127 while (i !== iLength && diffs[i][0] === DIFF_EQUAL) {
128 i += 1;
129 }
130
131 if (iStart !== i) {
132 if (iStart === 0) {
133 // at start
134 if (i > nContextLines) {
135 jLength -= i - nContextLines; // subtract excess common lines
136 hasExcessAtStartOrEnd = true;
137 }
138 } else if (i === iLength) {
139 // at end
140 const n = i - iStart;
141 if (n > nContextLines) {
142 jLength -= n - nContextLines; // subtract excess common lines
143 hasExcessAtStartOrEnd = true;
144 }
145 } else {
146 // between changes
147 const n = i - iStart;
148 if (n > nContextLines2) {
149 jLength -= n - nContextLines2; // subtract excess common lines
150 nExcessesBetweenChanges += 1;
151 }
152 }
153 }
154
155 while (i !== iLength && diffs[i][0] !== DIFF_EQUAL) {
156 i += 1;
157 }
158 }
159
160 const hasPatch = nExcessesBetweenChanges !== 0 || hasExcessAtStartOrEnd;
161 if (nExcessesBetweenChanges !== 0) {
162 jLength += nExcessesBetweenChanges + 1; // add patch lines
163 } else if (hasExcessAtStartOrEnd) {
164 jLength += 1; // add patch line
165 }
166 const jLast = jLength - 1;
167
168 const lines: Array<string> = [];
169

Callers 2

printDiffLinesFunction · 0.90

Calls 4

pushCommonLineFunction · 0.85
createPatchMarkFunction · 0.85
pushDeleteLineFunction · 0.85
pushInsertLineFunction · 0.85

Tested by

no test coverage detected