jest-diff
Advanced tools
Comparing version 29.1.2 to 29.2.0
@@ -12,3 +12,2 @@ 'use strict'; | ||
void 0; | ||
/** | ||
@@ -58,2 +57,3 @@ * Diff Match and Patch | ||
var DIFF_EQUAL = 0; | ||
/** | ||
@@ -66,9 +66,6 @@ * Class representing one diff tuple. | ||
*/ | ||
exports.DIFF_EQUAL = DIFF_EQUAL; | ||
class Diff { | ||
0; | ||
1; | ||
constructor(op, text) { | ||
@@ -79,2 +76,3 @@ this[0] = op; | ||
} | ||
/** | ||
@@ -87,5 +85,3 @@ * Determine the common prefix of two strings. | ||
*/ | ||
exports.Diff = Diff; | ||
var diff_commonPrefix = function (text1, text2) { | ||
@@ -95,5 +91,5 @@ // Quick check for common null cases. | ||
return 0; | ||
} // Binary search. | ||
} | ||
// Binary search. | ||
// Performance analysis: https://neil.fraser.name/news/2007/10/09/ | ||
var pointermin = 0; | ||
@@ -103,3 +99,2 @@ var pointermax = Math.min(text1.length, text2.length); | ||
var pointerstart = 0; | ||
while (pointermin < pointermid) { | ||
@@ -115,8 +110,7 @@ if ( | ||
} | ||
pointermid = Math.floor((pointermax - pointermin) / 2 + pointermin); | ||
} | ||
return pointermid; | ||
}; | ||
/** | ||
@@ -128,3 +122,2 @@ * Determine the common suffix of two strings. | ||
*/ | ||
var diff_commonSuffix = function (text1, text2) { | ||
@@ -138,5 +131,5 @@ // Quick check for common null cases. | ||
return 0; | ||
} // Binary search. | ||
} | ||
// Binary search. | ||
// Performance analysis: https://neil.fraser.name/news/2007/10/09/ | ||
var pointermin = 0; | ||
@@ -146,3 +139,2 @@ var pointermax = Math.min(text1.length, text2.length); | ||
var pointerend = 0; | ||
while (pointermin < pointermid) { | ||
@@ -158,8 +150,7 @@ if ( | ||
} | ||
pointermid = Math.floor((pointermax - pointermin) / 2 + pointermin); | ||
} | ||
return pointermid; | ||
}; | ||
/** | ||
@@ -173,12 +164,11 @@ * Determine if the suffix of one string is the prefix of another. | ||
*/ | ||
var diff_commonOverlap_ = function (text1, text2) { | ||
// Cache the text lengths to prevent multiple calls. | ||
var text1_length = text1.length; | ||
var text2_length = text2.length; // Eliminate the null case. | ||
var text2_length = text2.length; | ||
// Eliminate the null case. | ||
if (text1_length == 0 || text2_length == 0) { | ||
return 0; | ||
} // Truncate the longer string. | ||
} | ||
// Truncate the longer string. | ||
if (text1_length > text2_length) { | ||
@@ -189,24 +179,20 @@ text1 = text1.substring(text1_length - text2_length); | ||
} | ||
var text_length = Math.min(text1_length, text2_length); // Quick check for the worst case. | ||
var text_length = Math.min(text1_length, text2_length); | ||
// Quick check for the worst case. | ||
if (text1 == text2) { | ||
return text_length; | ||
} // Start by looking for a single character match | ||
} | ||
// Start by looking for a single character match | ||
// and increase length until no match is found. | ||
// Performance analysis: https://neil.fraser.name/news/2010/11/04/ | ||
var best = 0; | ||
var length = 1; | ||
while (true) { | ||
var pattern = text1.substring(text_length - length); | ||
var found = text2.indexOf(pattern); | ||
if (found == -1) { | ||
return best; | ||
} | ||
length += found; | ||
if ( | ||
@@ -221,2 +207,3 @@ found == 0 || | ||
}; | ||
/** | ||
@@ -226,22 +213,16 @@ * Reduce the number of edits by eliminating semantically trivial equalities. | ||
*/ | ||
var diff_cleanupSemantic = function (diffs) { | ||
var changes = false; | ||
var equalities = []; // Stack of indices where equalities are found. | ||
var equalitiesLength = 0; // Keeping our own length var is faster in JS. | ||
/** @type {?string} */ | ||
var lastEquality = null; // Always equal to diffs[equalities[equalitiesLength - 1]][1] | ||
var lastEquality = null; | ||
// Always equal to diffs[equalities[equalitiesLength - 1]][1] | ||
var pointer = 0; // Index of current position. | ||
// Number of characters that changed prior to the equality. | ||
var length_insertions1 = 0; | ||
var length_deletions1 = 0; // Number of characters that changed after the equality. | ||
var length_deletions1 = 0; | ||
// Number of characters that changed after the equality. | ||
var length_insertions2 = 0; | ||
var length_deletions2 = 0; | ||
while (pointer < diffs.length) { | ||
@@ -262,5 +243,5 @@ if (diffs[pointer][0] == DIFF_EQUAL) { | ||
length_deletions2 += diffs[pointer][1].length; | ||
} // Eliminate an equality that is smaller or equal to the edits on both | ||
} | ||
// Eliminate an equality that is smaller or equal to the edits on both | ||
// sides of it. | ||
if ( | ||
@@ -277,12 +258,11 @@ lastEquality && | ||
new Diff(DIFF_DELETE, lastEquality) | ||
); // Change second copy to insert. | ||
diffs[equalities[equalitiesLength - 1] + 1][0] = DIFF_INSERT; // Throw away the equality we just deleted. | ||
equalitiesLength--; // Throw away the previous equality (it needs to be reevaluated). | ||
); | ||
// Change second copy to insert. | ||
diffs[equalities[equalitiesLength - 1] + 1][0] = DIFF_INSERT; | ||
// Throw away the equality we just deleted. | ||
equalitiesLength--; | ||
// Throw away the previous equality (it needs to be reevaluated). | ||
equalitiesLength--; | ||
pointer = equalitiesLength > 0 ? equalities[equalitiesLength - 1] : -1; | ||
length_insertions1 = 0; // Reset the counters. | ||
length_deletions1 = 0; | ||
@@ -295,11 +275,12 @@ length_insertions2 = 0; | ||
} | ||
pointer++; | ||
} // Normalize the diff. | ||
} | ||
// Normalize the diff. | ||
if (changes) { | ||
diff_cleanupMerge(diffs); | ||
} | ||
diff_cleanupSemanticLossless(diffs); | ||
diff_cleanupSemanticLossless(diffs); // Find any overlaps between deletions and insertions. | ||
// Find any overlaps between deletions and insertions. | ||
// e.g: <del>abcxxx</del><ins>xxxdef</ins> | ||
@@ -310,5 +291,3 @@ // -> <del>abc</del>xxx<ins>def</ins> | ||
// Only extract an overlap if it is as big as the edit ahead or behind it. | ||
pointer = 1; | ||
while (pointer < diffs.length) { | ||
@@ -323,3 +302,2 @@ if ( | ||
var overlap_length2 = diff_commonOverlap_(insertion, deletion); | ||
if (overlap_length1 >= overlap_length2) { | ||
@@ -365,9 +343,8 @@ if ( | ||
} | ||
pointer++; | ||
} | ||
pointer++; | ||
} | ||
}; | ||
/** | ||
@@ -379,5 +356,3 @@ * Look for single edits surrounded on both sides by equalities | ||
*/ | ||
exports.cleanupSemantic = diff_cleanupSemantic; | ||
var diff_cleanupSemanticLossless = function (diffs) { | ||
@@ -398,3 +373,5 @@ /** | ||
return 6; | ||
} // Each port of this function behaves slightly differently due to | ||
} | ||
// Each port of this function behaves slightly differently due to | ||
// subtle differences in each language's definition of things like | ||
@@ -404,3 +381,2 @@ // 'whitespace'. Since this function's purpose is largely cosmetic, | ||
// rather than force total conformity. | ||
var char1 = one.charAt(one.length - 1); | ||
@@ -416,3 +392,2 @@ var char2 = two.charAt(0); | ||
var blankLine2 = lineBreak2 && two.match(blanklineStartRegex_); | ||
if (blankLine1 || blankLine2) { | ||
@@ -434,8 +409,6 @@ // Five points for blank lines. | ||
} | ||
return 0; | ||
} | ||
var pointer = 1; // Intentionally ignore the first and last element (don't need checking). | ||
var pointer = 1; | ||
// Intentionally ignore the first and last element (don't need checking). | ||
while (pointer < diffs.length - 1) { | ||
@@ -449,6 +422,6 @@ if ( | ||
var edit = diffs[pointer][1]; | ||
var equality2 = diffs[pointer + 1][1]; // First, shift the edit as far left as possible. | ||
var equality2 = diffs[pointer + 1][1]; | ||
// First, shift the edit as far left as possible. | ||
var commonOffset = diff_commonSuffix(equality1, edit); | ||
if (commonOffset) { | ||
@@ -459,4 +432,5 @@ var commonString = edit.substring(edit.length - commonOffset); | ||
equality2 = commonString + equality2; | ||
} // Second, step character by character right, looking for the best fit. | ||
} | ||
// Second, step character by character right, looking for the best fit. | ||
var bestEquality1 = equality1; | ||
@@ -468,3 +442,2 @@ var bestEdit = edit; | ||
diff_cleanupSemanticScore_(edit, equality2); | ||
while (edit.charAt(0) === equality2.charAt(0)) { | ||
@@ -476,4 +449,4 @@ equality1 += edit.charAt(0); | ||
diff_cleanupSemanticScore_(equality1, edit) + | ||
diff_cleanupSemanticScore_(edit, equality2); // The >= encourages trailing rather than leading whitespace on edits. | ||
diff_cleanupSemanticScore_(edit, equality2); | ||
// The >= encourages trailing rather than leading whitespace on edits. | ||
if (score >= bestScore) { | ||
@@ -486,3 +459,2 @@ bestScore = score; | ||
} | ||
if (diffs[pointer - 1][1] != bestEquality1) { | ||
@@ -496,5 +468,3 @@ // We have an improvement, save it back to the diff. | ||
} | ||
diffs[pointer][1] = bestEdit; | ||
if (bestEquality2) { | ||
@@ -508,7 +478,7 @@ diffs[pointer + 1][1] = bestEquality2; | ||
} | ||
pointer++; | ||
} | ||
}; // Define some regex patterns for matching boundaries. | ||
}; | ||
// Define some regex patterns for matching boundaries. | ||
var nonAlphaNumericRegex_ = /[^a-zA-Z0-9]/; | ||
@@ -519,2 +489,3 @@ var whitespaceRegex_ = /\s/; | ||
var blanklineStartRegex_ = /^\r?\n\r?\n/; | ||
/** | ||
@@ -525,3 +496,2 @@ * Reorder and merge like edit sections. Merge equalities. | ||
*/ | ||
var diff_cleanupMerge = function (diffs) { | ||
@@ -536,3 +506,2 @@ // Add a dummy entry at the end. | ||
var commonlength; | ||
while (pointer < diffs.length) { | ||
@@ -545,3 +514,2 @@ switch (diffs[pointer][0]) { | ||
break; | ||
case DIFF_DELETE: | ||
@@ -552,3 +520,2 @@ count_delete++; | ||
break; | ||
case DIFF_EQUAL: | ||
@@ -560,3 +527,2 @@ // Upon reaching an equality, check for prior redundancies. | ||
commonlength = diff_commonPrefix(text_insert, text_delete); | ||
if (commonlength !== 0) { | ||
@@ -578,9 +544,7 @@ if ( | ||
} | ||
text_insert = text_insert.substring(commonlength); | ||
text_delete = text_delete.substring(commonlength); | ||
} // Factor out any common suffixies. | ||
} | ||
// Factor out any common suffixies. | ||
commonlength = diff_commonSuffix(text_insert, text_delete); | ||
if (commonlength !== 0) { | ||
@@ -599,7 +563,6 @@ diffs[pointer][1] = | ||
} | ||
} // Delete the offending records and add the merged ones. | ||
} | ||
// Delete the offending records and add the merged ones. | ||
pointer -= count_delete + count_insert; | ||
diffs.splice(pointer, count_delete + count_insert); | ||
if (text_delete.length) { | ||
@@ -609,3 +572,2 @@ diffs.splice(pointer, 0, new Diff(DIFF_DELETE, text_delete)); | ||
} | ||
if (text_insert.length) { | ||
@@ -615,3 +577,2 @@ diffs.splice(pointer, 0, new Diff(DIFF_INSERT, text_insert)); | ||
} | ||
pointer++; | ||
@@ -625,3 +586,2 @@ } else if (pointer !== 0 && diffs[pointer - 1][0] == DIFF_EQUAL) { | ||
} | ||
count_insert = 0; | ||
@@ -634,12 +594,12 @@ count_delete = 0; | ||
} | ||
if (diffs[diffs.length - 1][1] === '') { | ||
diffs.pop(); // Remove the dummy entry at the end. | ||
} // Second pass: look for single edits surrounded on both sides by equalities | ||
} | ||
// Second pass: look for single edits surrounded on both sides by equalities | ||
// which can be shifted sideways to eliminate an equality. | ||
// e.g: A<ins>BA</ins>C -> <ins>AB</ins>AC | ||
var changes = false; | ||
pointer = 1; // Intentionally ignore the first and last element (don't need checking). | ||
pointer = 1; | ||
// Intentionally ignore the first and last element (don't need checking). | ||
while (pointer < diffs.length - 1) { | ||
@@ -679,6 +639,5 @@ if ( | ||
} | ||
pointer++; | ||
} // If shifts were made, the diff needs reordering and another shift sweep. | ||
} | ||
// If shifts were made, the diff needs reordering and another shift sweep. | ||
if (changes) { | ||
@@ -685,0 +644,0 @@ diff_cleanupMerge(diffs); |
@@ -7,3 +7,2 @@ 'use strict'; | ||
exports.SIMILAR_MESSAGE = exports.NO_DIFF_MESSAGE = void 0; | ||
/** | ||
@@ -15,2 +14,3 @@ * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
*/ | ||
const NO_DIFF_MESSAGE = 'Compared values have no visual difference.'; | ||
@@ -17,0 +17,0 @@ exports.NO_DIFF_MESSAGE = NO_DIFF_MESSAGE; |
@@ -11,15 +11,9 @@ 'use strict'; | ||
void 0; | ||
var _diffSequences = _interopRequireDefault(require('diff-sequences')); | ||
var _cleanupSemantic = require('./cleanupSemantic'); | ||
var _joinAlignedDiffs = require('./joinAlignedDiffs'); | ||
var _normalizeDiffOptions = require('./normalizeDiffOptions'); | ||
function _interopRequireDefault(obj) { | ||
return obj && obj.__esModule ? obj : {default: obj}; | ||
} | ||
/** | ||
@@ -31,4 +25,4 @@ * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
*/ | ||
const isEmptyString = lines => lines.length === 1 && lines[0].length === 0; | ||
const countChanges = diffs => { | ||
@@ -42,3 +36,2 @@ let a = 0; | ||
break; | ||
case _cleanupSemantic.DIFF_INSERT: | ||
@@ -54,3 +47,2 @@ b += 1; | ||
}; | ||
const printAnnotation = ( | ||
@@ -72,14 +64,14 @@ { | ||
} | ||
let aRest = ''; | ||
let bRest = ''; | ||
if (includeChangeCounts) { | ||
const aCount = String(changeCounts.a); | ||
const bCount = String(changeCounts.b); // Padding right aligns the ends of the annotations. | ||
const bCount = String(changeCounts.b); | ||
// Padding right aligns the ends of the annotations. | ||
const baAnnotationLengthDiff = bAnnotation.length - aAnnotation.length; | ||
const aAnnotationPadding = ' '.repeat(Math.max(0, baAnnotationLengthDiff)); | ||
const bAnnotationPadding = ' '.repeat(Math.max(0, -baAnnotationLengthDiff)); // Padding left aligns the ends of the counts. | ||
const bAnnotationPadding = ' '.repeat(Math.max(0, -baAnnotationLengthDiff)); | ||
// Padding left aligns the ends of the counts. | ||
const baCountLengthDiff = bCount.length - aCount.length; | ||
@@ -91,3 +83,2 @@ const aCountPadding = ' '.repeat(Math.max(0, baCountLengthDiff)); | ||
} | ||
const a = `${aIndicator} ${aAnnotation}${aRest}`; | ||
@@ -97,3 +88,2 @@ const b = `${bIndicator} ${bAnnotation}${bRest}`; | ||
}; | ||
const printDiffLines = (diffs, options) => | ||
@@ -103,6 +93,6 @@ printAnnotation(options, countChanges(diffs)) + | ||
? (0, _joinAlignedDiffs.joinAlignedDiffsExpand)(diffs, options) | ||
: (0, _joinAlignedDiffs.joinAlignedDiffsNoExpand)(diffs, options)); // Compare two arrays of strings line-by-line. Format as comparison lines. | ||
: (0, _joinAlignedDiffs.joinAlignedDiffsNoExpand)(diffs, options)); | ||
// Compare two arrays of strings line-by-line. Format as comparison lines. | ||
exports.printDiffLines = printDiffLines; | ||
const diffLinesUnified = (aLines, bLines, options) => | ||
@@ -115,8 +105,8 @@ printDiffLines( | ||
(0, _normalizeDiffOptions.normalizeDiffOptions)(options) | ||
); // Given two pairs of arrays of strings: | ||
); | ||
// Given two pairs of arrays of strings: | ||
// Compare the pair of comparison arrays line-by-line. | ||
// Format the corresponding lines in the pair of displayable arrays. | ||
exports.diffLinesUnified = diffLinesUnified; | ||
const diffLinesUnified2 = ( | ||
@@ -133,3 +123,2 @@ aLinesDisplay, | ||
} | ||
if (isEmptyString(bLinesDisplay) && isEmptyString(bLinesCompare)) { | ||
@@ -139,3 +128,2 @@ bLinesDisplay = []; | ||
} | ||
if ( | ||
@@ -148,5 +136,5 @@ aLinesDisplay.length !== aLinesCompare.length || | ||
} | ||
const diffs = diffLinesRaw(aLinesCompare, bLinesCompare); | ||
const diffs = diffLinesRaw(aLinesCompare, bLinesCompare); // Replace comparison lines with displayable lines. | ||
// Replace comparison lines with displayable lines. | ||
let aIndex = 0; | ||
@@ -160,3 +148,2 @@ let bIndex = 0; | ||
break; | ||
case _cleanupSemantic.DIFF_INSERT: | ||
@@ -166,3 +153,2 @@ diff[1] = bLinesDisplay[bIndex]; | ||
break; | ||
default: | ||
@@ -178,16 +164,13 @@ diff[1] = bLinesDisplay[bIndex]; | ||
); | ||
}; // Compare two arrays of strings line-by-line. | ||
}; | ||
// Compare two arrays of strings line-by-line. | ||
exports.diffLinesUnified2 = diffLinesUnified2; | ||
const diffLinesRaw = (aLines, bLines) => { | ||
const aLength = aLines.length; | ||
const bLength = bLines.length; | ||
const isCommon = (aIndex, bIndex) => aLines[aIndex] === bLines[bIndex]; | ||
const diffs = []; | ||
let aIndex = 0; | ||
let bIndex = 0; | ||
const foundSubsequence = (nCommon, aCommon, bCommon) => { | ||
@@ -199,3 +182,2 @@ for (; aIndex !== aCommon; aIndex += 1) { | ||
} | ||
for (; bIndex !== bCommon; bIndex += 1) { | ||
@@ -206,3 +188,2 @@ diffs.push( | ||
} | ||
for (; nCommon !== 0; nCommon -= 1, aIndex += 1, bIndex += 1) { | ||
@@ -214,5 +195,5 @@ diffs.push( | ||
}; | ||
(0, _diffSequences.default)(aLength, bLength, isCommon, foundSubsequence); | ||
(0, _diffSequences.default)(aLength, bLength, isCommon, foundSubsequence); // After the last common subsequence, push remaining change items. | ||
// After the last common subsequence, push remaining change items. | ||
for (; aIndex !== aLength; aIndex += 1) { | ||
@@ -223,3 +204,2 @@ diffs.push( | ||
} | ||
for (; bIndex !== bLength; bIndex += 1) { | ||
@@ -230,6 +210,4 @@ diffs.push( | ||
} | ||
return diffs; | ||
}; | ||
exports.diffLinesRaw = diffLinesRaw; |
@@ -7,11 +7,7 @@ 'use strict'; | ||
exports.default = void 0; | ||
var _diffSequences = _interopRequireDefault(require('diff-sequences')); | ||
var _cleanupSemantic = require('./cleanupSemantic'); | ||
function _interopRequireDefault(obj) { | ||
return obj && obj.__esModule ? obj : {default: obj}; | ||
} | ||
/** | ||
@@ -23,9 +19,8 @@ * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
*/ | ||
const diffStrings = (a, b) => { | ||
const isCommon = (aIndex, bIndex) => a[aIndex] === b[bIndex]; | ||
let aIndex = 0; | ||
let bIndex = 0; | ||
const diffs = []; | ||
const foundSubsequence = (nCommon, aCommon, bCommon) => { | ||
@@ -40,3 +35,2 @@ if (aIndex !== aCommon) { | ||
} | ||
if (bIndex !== bCommon) { | ||
@@ -50,7 +44,4 @@ diffs.push( | ||
} | ||
aIndex = aCommon + nCommon; // number of characters compared in a | ||
bIndex = bCommon + nCommon; // number of characters compared in b | ||
diffs.push( | ||
@@ -63,5 +54,5 @@ new _cleanupSemantic.Diff( | ||
}; | ||
(0, _diffSequences.default)(a.length, b.length, isCommon, foundSubsequence); | ||
(0, _diffSequences.default)(a.length, b.length, isCommon, foundSubsequence); // After the last common subsequence, push remaining change items. | ||
// After the last common subsequence, push remaining change items. | ||
if (aIndex !== a.length) { | ||
@@ -72,3 +63,2 @@ diffs.push( | ||
} | ||
if (bIndex !== b.length) { | ||
@@ -79,7 +69,5 @@ diffs.push( | ||
} | ||
return diffs; | ||
}; | ||
var _default = diffStrings; | ||
exports.default = _default; |
@@ -7,5 +7,3 @@ 'use strict'; | ||
exports.default = void 0; | ||
var _cleanupSemantic = require('./cleanupSemantic'); | ||
/** | ||
@@ -17,2 +15,3 @@ * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
*/ | ||
// Given change op and array of diffs, return concatenated string: | ||
@@ -32,12 +31,10 @@ // * include common strings | ||
'' | ||
); // Encapsulate change lines until either a common newline or the end. | ||
); | ||
// Encapsulate change lines until either a common newline or the end. | ||
class ChangeBuffer { | ||
op; | ||
line; // incomplete line | ||
lines; // complete lines | ||
changeColor; | ||
constructor(op, changeColor) { | ||
@@ -49,10 +46,9 @@ this.op = op; | ||
} | ||
pushSubstring(substring) { | ||
this.pushDiff(new _cleanupSemantic.Diff(this.op, substring)); | ||
} | ||
pushLine() { | ||
// Assume call only if line has at least one diff, | ||
// therefore an empty line must have a diff which has an empty string. | ||
// If line has multiple diffs, then assume it has a common diff, | ||
@@ -71,16 +67,17 @@ // therefore change diffs have change color; | ||
); | ||
this.line.length = 0; | ||
} | ||
isLineEmpty() { | ||
return this.line.length === 0; | ||
} // Minor input to buffer. | ||
} | ||
// Minor input to buffer. | ||
pushDiff(diff) { | ||
this.line.push(diff); | ||
} // Main input to buffer. | ||
} | ||
// Main input to buffer. | ||
align(diff) { | ||
const string = diff[1]; | ||
if (string.includes('\n')) { | ||
@@ -106,4 +103,5 @@ const substrings = string.split('\n'); | ||
} | ||
} // Output from buffer. | ||
} | ||
// Output from buffer. | ||
moveLinesTo(lines) { | ||
@@ -113,8 +111,8 @@ if (!this.isLineEmpty()) { | ||
} | ||
lines.push(...this.lines); | ||
this.lines.length = 0; | ||
} | ||
} // Encapsulate common and change lines. | ||
} | ||
// Encapsulate common and change lines. | ||
class CommonBuffer { | ||
@@ -124,3 +122,2 @@ deleteBuffer; | ||
lines; | ||
constructor(deleteBuffer, insertBuffer) { | ||
@@ -131,14 +128,12 @@ this.deleteBuffer = deleteBuffer; | ||
} | ||
pushDiffCommonLine(diff) { | ||
this.lines.push(diff); | ||
} | ||
pushDiffChangeLines(diff) { | ||
const isDiffEmpty = diff[1].length === 0; // An empty diff string is redundant, unless a change line is empty. | ||
const isDiffEmpty = diff[1].length === 0; | ||
// An empty diff string is redundant, unless a change line is empty. | ||
if (!isDiffEmpty || this.deleteBuffer.isLineEmpty()) { | ||
this.deleteBuffer.pushDiff(diff); | ||
} | ||
if (!isDiffEmpty || this.insertBuffer.isLineEmpty()) { | ||
@@ -148,12 +143,11 @@ this.insertBuffer.pushDiff(diff); | ||
} | ||
flushChangeLines() { | ||
this.deleteBuffer.moveLinesTo(this.lines); | ||
this.insertBuffer.moveLinesTo(this.lines); | ||
} // Input to buffer. | ||
} | ||
// Input to buffer. | ||
align(diff) { | ||
const op = diff[0]; | ||
const string = diff[1]; | ||
if (string.includes('\n')) { | ||
@@ -165,3 +159,2 @@ const substrings = string.split('\n'); | ||
const subdiff = new _cleanupSemantic.Diff(op, substring); | ||
if ( | ||
@@ -197,4 +190,5 @@ this.deleteBuffer.isLineEmpty() && | ||
} | ||
} // Output from buffer. | ||
} | ||
// Output from buffer. | ||
getLines() { | ||
@@ -204,3 +198,5 @@ this.flushChangeLines(); | ||
} | ||
} // Given diffs from expected and received strings, | ||
} | ||
// Given diffs from expected and received strings, | ||
// return new array of diffs split or joined into lines. | ||
@@ -215,3 +211,2 @@ // | ||
// * if neither expected nor received is multiline string | ||
const getAlignedDiffs = (diffs, changeColor) => { | ||
@@ -232,7 +227,5 @@ const deleteBuffer = new ChangeBuffer( | ||
break; | ||
case _cleanupSemantic.DIFF_INSERT: | ||
insertBuffer.align(diff); | ||
break; | ||
default: | ||
@@ -244,4 +237,3 @@ commonBuffer.align(diff); | ||
}; | ||
var _default = getAlignedDiffs; | ||
exports.default = _default; |
@@ -61,25 +61,14 @@ 'use strict'; | ||
}); | ||
var _chalk = _interopRequireDefault(require('chalk')); | ||
var _jestGetType = require('jest-get-type'); | ||
var _prettyFormat = require('pretty-format'); | ||
var _cleanupSemantic = require('./cleanupSemantic'); | ||
var _constants = require('./constants'); | ||
var _diffLines = require('./diffLines'); | ||
var _normalizeDiffOptions = require('./normalizeDiffOptions'); | ||
var _printDiffs = require('./printDiffs'); | ||
function _interopRequireDefault(obj) { | ||
return obj && obj.__esModule ? obj : {default: obj}; | ||
} | ||
var Symbol = globalThis['jest-symbol-do-not-touch'] || globalThis.Symbol; | ||
const getCommonMessage = (message, options) => { | ||
@@ -91,3 +80,2 @@ const {commonColor} = (0, _normalizeDiffOptions.normalizeDiffOptions)( | ||
}; | ||
const { | ||
@@ -116,6 +104,7 @@ AsymmetricMatcher, | ||
plugins: PLUGINS | ||
}; // Generate a string that will highlight the difference between two values | ||
}; | ||
// Generate a string that will highlight the difference between two values | ||
// with green and red. (similar to how github does code diffing) | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
function diff(a, b, options) { | ||
@@ -125,7 +114,5 @@ if (Object.is(a, b)) { | ||
} | ||
const aType = (0, _jestGetType.getType)(a); | ||
let expectedType = aType; | ||
let omitDifference = false; | ||
if (aType === 'object' && typeof a.asymmetricMatch === 'function') { | ||
@@ -136,3 +123,2 @@ if (a.$$typeof !== Symbol.for('jest.asymmetricMatcher')) { | ||
} | ||
if (typeof a.getExpectedType !== 'function') { | ||
@@ -142,9 +128,7 @@ // For example, expect.anything() matches either null or undefined | ||
} | ||
expectedType = a.getExpectedType(); // Primitive types boolean and number omit difference below. | ||
expectedType = a.getExpectedType(); | ||
// Primitive types boolean and number omit difference below. | ||
// For example, omit difference for expect.stringMatching(regexp) | ||
omitDifference = expectedType === 'string'; | ||
} | ||
if (expectedType !== (0, _jestGetType.getType)(b)) { | ||
@@ -157,7 +141,5 @@ return ( | ||
} | ||
if (omitDifference) { | ||
return null; | ||
} | ||
switch (aType) { | ||
@@ -170,13 +152,9 @@ case 'string': | ||
); | ||
case 'boolean': | ||
case 'number': | ||
return comparePrimitive(a, b, options); | ||
case 'map': | ||
return compareObjects(sortMap(a), sortMap(b), options); | ||
case 'set': | ||
return compareObjects(sortSet(a), sortSet(b), options); | ||
default: | ||
@@ -186,3 +164,2 @@ return compareObjects(a, b, options); | ||
} | ||
function comparePrimitive(a, b, options) { | ||
@@ -199,15 +176,11 @@ const aFormat = (0, _prettyFormat.format)(a, FORMAT_OPTIONS); | ||
} | ||
function sortMap(map) { | ||
return new Map(Array.from(map.entries()).sort()); | ||
} | ||
function sortSet(set) { | ||
return new Set(Array.from(set.values()).sort()); | ||
} | ||
function compareObjects(a, b, options) { | ||
let difference; | ||
let hasThrown = false; | ||
try { | ||
@@ -219,10 +192,8 @@ const formatOptions = getFormatOptions(FORMAT_OPTIONS, options); | ||
} | ||
const noDiffMessage = getCommonMessage(_constants.NO_DIFF_MESSAGE, options); // If the comparison yields no results, compare again but this time | ||
const noDiffMessage = getCommonMessage(_constants.NO_DIFF_MESSAGE, options); | ||
// If the comparison yields no results, compare again but this time | ||
// without calling `toJSON`. It's also possible that toJSON might throw. | ||
if (difference === undefined || difference === noDiffMessage) { | ||
const formatOptions = getFormatOptions(FALLBACK_FORMAT_OPTIONS, options); | ||
difference = getObjectsDifference(a, b, formatOptions, options); | ||
if (difference !== noDiffMessage && !hasThrown) { | ||
@@ -235,6 +206,4 @@ difference = `${getCommonMessage( | ||
} | ||
return difference; | ||
} | ||
function getFormatOptions(formatOptions, options) { | ||
@@ -244,10 +213,14 @@ const {compareKeys} = (0, _normalizeDiffOptions.normalizeDiffOptions)( | ||
); | ||
return {...formatOptions, compareKeys}; | ||
return { | ||
...formatOptions, | ||
compareKeys | ||
}; | ||
} | ||
function getObjectsDifference(a, b, formatOptions, options) { | ||
const formatOptionsZeroIndent = {...formatOptions, indent: 0}; | ||
const formatOptionsZeroIndent = { | ||
...formatOptions, | ||
indent: 0 | ||
}; | ||
const aCompare = (0, _prettyFormat.format)(a, formatOptionsZeroIndent); | ||
const bCompare = (0, _prettyFormat.format)(b, formatOptionsZeroIndent); | ||
if (aCompare === bCompare) { | ||
@@ -254,0 +227,0 @@ return getCommonMessage(_constants.NO_DIFF_MESSAGE, options); |
@@ -7,5 +7,3 @@ 'use strict'; | ||
exports.joinAlignedDiffsNoExpand = exports.joinAlignedDiffsExpand = void 0; | ||
var _cleanupSemantic = require('./cleanupSemantic'); | ||
/** | ||
@@ -17,5 +15,5 @@ * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
*/ | ||
const formatTrailingSpaces = (line, trailingSpaceFormatter) => | ||
line.replace(/\s+$/, match => trailingSpaceFormatter(match)); | ||
const printDiffLine = ( | ||
@@ -38,3 +36,2 @@ line, | ||
: ''; | ||
const printDeleteLine = ( | ||
@@ -58,3 +55,2 @@ line, | ||
); | ||
const printInsertLine = ( | ||
@@ -78,3 +74,2 @@ line, | ||
); | ||
const printCommonLine = ( | ||
@@ -97,17 +92,20 @@ line, | ||
emptyFirstOrLastLinePlaceholder | ||
); // In GNU diff format, indexes are one-based instead of zero-based. | ||
); | ||
// In GNU diff format, indexes are one-based instead of zero-based. | ||
const createPatchMark = (aStart, aEnd, bStart, bEnd, {patchColor}) => | ||
patchColor( | ||
`@@ -${aStart + 1},${aEnd - aStart} +${bStart + 1},${bEnd - bStart} @@` | ||
); // jest --no-expand | ||
); | ||
// jest --no-expand | ||
// | ||
// Given array of aligned strings with inverse highlight formatting, | ||
// return joined lines with diff formatting (and patch marks, if needed). | ||
const joinAlignedDiffsNoExpand = (diffs, options) => { | ||
const iLength = diffs.length; | ||
const nContextLines = options.contextLines; | ||
const nContextLines2 = nContextLines + nContextLines; // First pass: count output lines and see if it has patches. | ||
const nContextLines2 = nContextLines + nContextLines; | ||
// First pass: count output lines and see if it has patches. | ||
let jLength = iLength; | ||
@@ -117,10 +115,7 @@ let hasExcessAtStartOrEnd = false; | ||
let i = 0; | ||
while (i !== iLength) { | ||
const iStart = i; | ||
while (i !== iLength && diffs[i][0] === _cleanupSemantic.DIFF_EQUAL) { | ||
i += 1; | ||
} | ||
if (iStart !== i) { | ||
@@ -131,3 +126,2 @@ if (iStart === 0) { | ||
jLength -= i - nContextLines; // subtract excess common lines | ||
hasExcessAtStartOrEnd = true; | ||
@@ -138,6 +132,4 @@ } | ||
const n = i - iStart; | ||
if (n > nContextLines) { | ||
jLength -= n - nContextLines; // subtract excess common lines | ||
hasExcessAtStartOrEnd = true; | ||
@@ -148,6 +140,4 @@ } | ||
const n = i - iStart; | ||
if (n > nContextLines2) { | ||
jLength -= n - nContextLines2; // subtract excess common lines | ||
nExcessesBetweenChanges += 1; | ||
@@ -157,3 +147,2 @@ } | ||
} | ||
while (i !== iLength && diffs[i][0] !== _cleanupSemantic.DIFF_EQUAL) { | ||
@@ -163,5 +152,3 @@ i += 1; | ||
} | ||
const hasPatch = nExcessesBetweenChanges !== 0 || hasExcessAtStartOrEnd; | ||
if (nExcessesBetweenChanges !== 0) { | ||
@@ -176,7 +163,7 @@ jLength += nExcessesBetweenChanges + 1; // add patch lines | ||
let jPatchMark = 0; // index of placeholder line for current patch mark | ||
if (hasPatch) { | ||
lines.push(''); // placeholder line for first patch mark | ||
} // Indexes of expected or received lines in current patch: | ||
} | ||
// Indexes of expected or received lines in current patch: | ||
let aStart = 0; | ||
@@ -186,3 +173,2 @@ let bStart = 0; | ||
let bEnd = 0; | ||
const pushCommonLine = line => { | ||
@@ -194,3 +180,2 @@ const j = lines.length; | ||
}; | ||
const pushDeleteLine = line => { | ||
@@ -201,3 +186,2 @@ const j = lines.length; | ||
}; | ||
const pushInsertLine = line => { | ||
@@ -207,13 +191,11 @@ const j = lines.length; | ||
bEnd += 1; | ||
}; // Second pass: push lines with diff formatting (and patch marks, if needed). | ||
}; | ||
// Second pass: push lines with diff formatting (and patch marks, if needed). | ||
i = 0; | ||
while (i !== iLength) { | ||
let iStart = i; | ||
while (i !== iLength && diffs[i][0] === _cleanupSemantic.DIFF_EQUAL) { | ||
i += 1; | ||
} | ||
if (iStart !== i) { | ||
@@ -229,3 +211,2 @@ if (iStart === 0) { | ||
} | ||
for (let iCommon = iStart; iCommon !== i; iCommon += 1) { | ||
@@ -237,3 +218,2 @@ pushCommonLine(diffs[iCommon][1]); | ||
const iEnd = i - iStart > nContextLines ? iStart + nContextLines : i; | ||
for (let iCommon = iStart; iCommon !== iEnd; iCommon += 1) { | ||
@@ -245,10 +225,7 @@ pushCommonLine(diffs[iCommon][1]); | ||
const nCommon = i - iStart; | ||
if (nCommon > nContextLines2) { | ||
const iEnd = iStart + nContextLines; | ||
for (let iCommon = iStart; iCommon !== iEnd; iCommon += 1) { | ||
pushCommonLine(diffs[iCommon][1]); | ||
} | ||
lines[jPatchMark] = createPatchMark( | ||
@@ -269,3 +246,2 @@ aStart, | ||
bEnd = bStart; | ||
for (let iCommon = i - nContextLines; iCommon !== i; iCommon += 1) { | ||
@@ -281,3 +257,2 @@ pushCommonLine(diffs[iCommon][1]); | ||
} | ||
while (i !== iLength && diffs[i][0] === _cleanupSemantic.DIFF_DELETE) { | ||
@@ -287,3 +262,2 @@ pushDeleteLine(diffs[i][1]); | ||
} | ||
while (i !== iLength && diffs[i][0] === _cleanupSemantic.DIFF_INSERT) { | ||
@@ -294,15 +268,13 @@ pushInsertLine(diffs[i][1]); | ||
} | ||
if (hasPatch) { | ||
lines[jPatchMark] = createPatchMark(aStart, aEnd, bStart, bEnd, options); | ||
} | ||
return lines.join('\n'); | ||
}; | ||
return lines.join('\n'); | ||
}; // jest --expand | ||
// jest --expand | ||
// | ||
// Given array of aligned strings with inverse highlight formatting, | ||
// return joined lines with diff formatting. | ||
exports.joinAlignedDiffsNoExpand = joinAlignedDiffsNoExpand; | ||
const joinAlignedDiffsExpand = (diffs, options) => | ||
@@ -313,10 +285,7 @@ diffs | ||
const isFirstOrLast = i === 0 || i === diffs.length - 1; | ||
switch (diff[0]) { | ||
case _cleanupSemantic.DIFF_DELETE: | ||
return printDeleteLine(line, isFirstOrLast, options); | ||
case _cleanupSemantic.DIFF_INSERT: | ||
return printInsertLine(line, isFirstOrLast, options); | ||
default: | ||
@@ -327,3 +296,2 @@ return printCommonLine(line, isFirstOrLast, options); | ||
.join('\n'); | ||
exports.joinAlignedDiffsExpand = joinAlignedDiffsExpand; |
@@ -7,9 +7,6 @@ 'use strict'; | ||
exports.normalizeDiffOptions = exports.noColor = void 0; | ||
var _chalk = _interopRequireDefault(require('chalk')); | ||
function _interopRequireDefault(obj) { | ||
return obj && obj.__esModule ? obj : {default: obj}; | ||
} | ||
/** | ||
@@ -21,4 +18,4 @@ * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
*/ | ||
const noColor = string => string; | ||
exports.noColor = noColor; | ||
@@ -46,3 +43,2 @@ const DIFF_CONTEXT_DEFAULT = 5; | ||
}; | ||
const getCompareKeys = compareKeys => | ||
@@ -52,3 +48,2 @@ compareKeys && typeof compareKeys === 'function' | ||
: OPTIONS_DEFAULT.compareKeys; | ||
const getContextLines = contextLines => | ||
@@ -59,4 +54,5 @@ typeof contextLines === 'number' && | ||
? contextLines | ||
: DIFF_CONTEXT_DEFAULT; // Pure function returns options with all properties. | ||
: DIFF_CONTEXT_DEFAULT; | ||
// Pure function returns options with all properties. | ||
const normalizeDiffOptions = (options = {}) => ({ | ||
@@ -68,3 +64,2 @@ ...OPTIONS_DEFAULT, | ||
}); | ||
exports.normalizeDiffOptions = normalizeDiffOptions; |
@@ -7,17 +7,10 @@ 'use strict'; | ||
exports.diffStringsUnified = exports.diffStringsRaw = void 0; | ||
var _cleanupSemantic = require('./cleanupSemantic'); | ||
var _diffLines = require('./diffLines'); | ||
var _diffStrings = _interopRequireDefault(require('./diffStrings')); | ||
var _getAlignedDiffs = _interopRequireDefault(require('./getAlignedDiffs')); | ||
var _normalizeDiffOptions = require('./normalizeDiffOptions'); | ||
function _interopRequireDefault(obj) { | ||
return obj && obj.__esModule ? obj : {default: obj}; | ||
} | ||
/** | ||
@@ -29,2 +22,3 @@ * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
*/ | ||
const hasCommonDiff = (diffs, isMultiline) => { | ||
@@ -40,11 +34,12 @@ if (isMultiline) { | ||
} | ||
return diffs.some(diff => diff[0] === _cleanupSemantic.DIFF_EQUAL); | ||
}; | ||
return diffs.some(diff => diff[0] === _cleanupSemantic.DIFF_EQUAL); | ||
}; // Compare two strings character-by-character. | ||
// Compare two strings character-by-character. | ||
// Format as comparison lines in which changed substrings have inverse colors. | ||
const diffStringsUnified = (a, b, options) => { | ||
if (a !== b && a.length !== 0 && b.length !== 0) { | ||
const isMultiline = a.includes('\n') || b.includes('\n'); // getAlignedDiffs assumes that a newline was appended to the strings. | ||
const isMultiline = a.includes('\n') || b.includes('\n'); | ||
// getAlignedDiffs assumes that a newline was appended to the strings. | ||
const diffs = diffStringsRaw( | ||
@@ -66,4 +61,5 @@ isMultiline ? `${a}\n` : a, | ||
} | ||
} // Fall back to line-by-line diff. | ||
} | ||
// Fall back to line-by-line diff. | ||
return (0, _diffLines.diffLinesUnified)( | ||
@@ -74,10 +70,9 @@ a.split('\n'), | ||
); | ||
}; // Compare two strings character-by-character. | ||
}; | ||
// Compare two strings character-by-character. | ||
// Optionally clean up small common substrings, also known as chaff. | ||
exports.diffStringsUnified = diffStringsUnified; | ||
const diffStringsRaw = (a, b, cleanup) => { | ||
const diffs = (0, _diffStrings.default)(a, b); | ||
if (cleanup) { | ||
@@ -89,3 +84,2 @@ (0, _cleanupSemantic.cleanupSemantic)(diffs); // impure function | ||
}; | ||
exports.diffStringsRaw = diffStringsRaw; |
{ | ||
"name": "jest-diff", | ||
"version": "29.1.2", | ||
"version": "29.2.0", | ||
"repository": { | ||
@@ -21,8 +21,8 @@ "type": "git", | ||
"chalk": "^4.0.0", | ||
"diff-sequences": "^29.0.0", | ||
"jest-get-type": "^29.0.0", | ||
"pretty-format": "^29.1.2" | ||
"diff-sequences": "^29.2.0", | ||
"jest-get-type": "^29.2.0", | ||
"pretty-format": "^29.2.0" | ||
}, | ||
"devDependencies": { | ||
"@jest/test-utils": "^29.1.2", | ||
"@jest/test-utils": "^29.2.0", | ||
"strip-ansi": "^6.0.0" | ||
@@ -36,3 +36,3 @@ }, | ||
}, | ||
"gitHead": "3c31dd619e8c022cde53f40fa12ea2a67f4752ce" | ||
"gitHead": "ee5b37a4f4433afcfffb0356cea47739d8092287" | ||
} |
1747
78499
Updateddiff-sequences@^29.2.0
Updatedjest-get-type@^29.2.0
Updatedpretty-format@^29.2.0