| 27 | // Compare two strings character-by-character. |
| 28 | // Format as comparison lines in which changed substrings have inverse colors. |
| 29 | export 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. |