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

Function diffStringsUnified

packages/jest-diff/src/printDiffs.ts:29–53  ·  view source on GitHub ↗
(
  a: string,
  b: string,
  options?: DiffOptions,
)

Source from the content-addressed store, hash-verified

27// Compare two strings character-by-character.
28// Format as comparison lines in which changed substrings have inverse colors.
29export const diffStringsUnified = (
30 a: string,
31 b: string,
32 options?: DiffOptions,
33): string => {
34 if (a !== b && a.length > 0 && b.length > 0) {
35 const isMultiline = a.includes('\n') || b.includes('\n');
36
37 // getAlignedDiffs assumes that a newline was appended to the strings.
38 const diffs = diffStringsRaw(
39 isMultiline ? `${a}\n` : a,
40 isMultiline ? `${b}\n` : b,
41 true, // cleanupSemantic
42 );
43
44 if (hasCommonDiff(diffs, isMultiline)) {
45 const optionsNormalized = normalizeDiffOptions(options);
46 const lines = getAlignedDiffs(diffs, optionsNormalized.changeColor);
47 return printDiffLines(lines, optionsNormalized);
48 }
49 }
50
51 // Fall back to line-by-line diff.
52 return diffLinesUnified(a.split('\n'), b.split('\n'), options);
53};
54
55// Compare two strings character-by-character.
56// Optionally clean up small common substrings, also known as chaff.

Callers 4

testAlignedDiffsFunction · 0.90
diff.test.tsFile · 0.90
printDiffOrStringifyFunction · 0.90
printSnapshotAndReceivedFunction · 0.90

Calls 6

normalizeDiffOptionsFunction · 0.90
printDiffLinesFunction · 0.90
diffLinesUnifiedFunction · 0.90
diffStringsRawFunction · 0.85
hasCommonDiffFunction · 0.85
getAlignedDiffsFunction · 0.85

Tested by 1

testAlignedDiffsFunction · 0.72