| 318 | const MAX_DIFF_STRING_LENGTH = 20_000; |
| 319 | |
| 320 | export const printDiffOrStringify = ( |
| 321 | expected: unknown, |
| 322 | received: unknown, |
| 323 | expectedLabel: string, |
| 324 | receivedLabel: string, |
| 325 | expand: boolean, class="cm">// CLI options: true if `--expand` or false if `--no-expand` |
| 326 | ): string => { |
| 327 | if ( |
| 328 | typeof expected === class="st">'string' && |
| 329 | typeof received === class="st">'string' && |
| 330 | expected.length > 0 && |
| 331 | received.length > 0 && |
| 332 | expected.length <= MAX_DIFF_STRING_LENGTH && |
| 333 | received.length <= MAX_DIFF_STRING_LENGTH && |
| 334 | expected !== received |
| 335 | ) { |
| 336 | if (expected.includes(class="st">'\n') || received.includes(class="st">'\n')) { |
| 337 | return diffStringsUnified(expected, received, { |
| 338 | aAnnotation: expectedLabel, |
| 339 | bAnnotation: receivedLabel, |
| 340 | changeLineTrailingSpaceColor: chalk.bgYellow, |
| 341 | commonLineTrailingSpaceColor: chalk.bgYellow, |
| 342 | emptyFirstOrLastLinePlaceholder: class="st">'↵', class="cm">// U+21B5 |
| 343 | expand, |
| 344 | includeChangeCounts: true, |
| 345 | }); |
| 346 | } |
| 347 | |
| 348 | const diffs = diffStringsRaw(expected, received, true); |
| 349 | const hasCommonDiff = diffs.some(diff => diff[0] === DIFF_EQUAL); |
| 350 | |
| 351 | const printLabel = getLabelPrinter(expectedLabel, receivedLabel); |
| 352 | const expectedLine = |
| 353 | printLabel(expectedLabel) + |
| 354 | printExpected( |
| 355 | getCommonAndChangedSubstrings(diffs, DIFF_DELETE, hasCommonDiff), |
| 356 | ); |
| 357 | const receivedLine = |
| 358 | printLabel(receivedLabel) + |
| 359 | printReceived( |
| 360 | getCommonAndChangedSubstrings(diffs, DIFF_INSERT, hasCommonDiff), |
| 361 | ); |
| 362 | |
| 363 | return `${expectedLine}\n${receivedLine}`; |
| 364 | } |
| 365 | |
| 366 | if (isLineDiffable(expected, received)) { |
| 367 | const {replacedExpected, replacedReceived} = |
| 368 | replaceMatchedToAsymmetricMatcher(expected, received, [], []); |
| 369 | const difference = diffDefault(replacedExpected, replacedReceived, { |
| 370 | aAnnotation: expectedLabel, |
| 371 | bAnnotation: receivedLabel, |
| 372 | expand, |
| 373 | includeChangeCounts: true, |
| 374 | }); |
| 375 | |
| 376 | if ( |
| 377 | typeof difference === class="st">'string' && |