MCPcopy
hub / github.com/vitest-dev/vitest / diffLinesRaw

Function diffLinesRaw

packages/utils/src/diff/diffLines.ts:178–233  ·  view source on GitHub ↗
(
  aLines: Array<string>,
  bLines: Array<string>,
  options?: DiffOptions,
)

Source from the content-addressed store, hash-verified

176
177// Compare two arrays of strings line-by-line.
178export function diffLinesRaw(
179 aLines: Array<string>,
180 bLines: Array<string>,
181 options?: DiffOptions,
182): [Array<Diff>, boolean] {
183 const truncate = options?.truncateThreshold ?? false
184 const truncateThreshold = Math.max(
185 Math.floor(options?.truncateThreshold ?? 0),
186 0,
187 )
188 const aLength = truncate
189 ? Math.min(aLines.length, truncateThreshold)
190 : aLines.length
191 const bLength = truncate
192 ? Math.min(bLines.length, truncateThreshold)
193 : bLines.length
194 const truncated = aLength !== aLines.length || bLength !== bLines.length
195
196 const isCommon = (aIndex: number, bIndex: number) =>
197 aLines[aIndex] === bLines[bIndex]
198
199 const diffs: Array<Diff> = []
200 let aIndex = 0
201 let bIndex = 0
202
203 const foundSubsequence = (
204 nCommon: number,
205 aCommon: number,
206 bCommon: number,
207 ) => {
208 for (; aIndex !== aCommon; aIndex += 1) {
209 diffs.push(new Diff(DIFF_DELETE, aLines[aIndex]))
210 }
211
212 for (; bIndex !== bCommon; bIndex += 1) {
213 diffs.push(new Diff(DIFF_INSERT, bLines[bIndex]))
214 }
215
216 for (; nCommon !== 0; nCommon -= 1, aIndex += 1, bIndex += 1) {
217 diffs.push(new Diff(DIFF_EQUAL, bLines[bIndex]))
218 }
219 }
220
221 diffSequences(aLength, bLength, isCommon, foundSubsequence)
222
223 // After the last common subsequence, push remaining change items.
224 for (; aIndex !== aLength; aIndex += 1) {
225 diffs.push(new Diff(DIFF_DELETE, aLines[aIndex]))
226 }
227
228 for (; bIndex !== bLength; bIndex += 1) {
229 diffs.push(new Diff(DIFF_INSERT, bLines[bIndex]))
230 }
231
232 return [diffs, truncated]
233}

Callers 2

diffLinesUnifiedFunction · 0.85
diffLinesUnified2Function · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected