jest-diff
Advanced tools
Comparing version 27.3.1 to 27.4.0
@@ -8,5 +8,6 @@ /** | ||
import { Diff } from './cleanupSemantic'; | ||
import type { DiffOptions } from './types'; | ||
import type { DiffOptions, DiffOptionsNormalized } from './types'; | ||
export declare const printDiffLines: (diffs: Array<Diff>, options: DiffOptionsNormalized) => string; | ||
export declare const diffLinesUnified: (aLines: Array<string>, bLines: Array<string>, options?: DiffOptions | undefined) => string; | ||
export declare const diffLinesUnified2: (aLinesDisplay: Array<string>, bLinesDisplay: Array<string>, aLinesCompare: Array<string>, bLinesCompare: Array<string>, options?: DiffOptions | undefined) => string; | ||
export declare const diffLinesRaw: (aLines: Array<string>, bLines: Array<string>) => Array<Diff>; |
@@ -9,2 +9,3 @@ 'use strict'; | ||
exports.diffLinesUnified = | ||
exports.printDiffLines = | ||
void 0; | ||
@@ -16,6 +17,6 @@ | ||
var _joinAlignedDiffs = require('./joinAlignedDiffs'); | ||
var _normalizeDiffOptions = require('./normalizeDiffOptions'); | ||
var _printDiffs = require('./printDiffs'); | ||
function _interopRequireDefault(obj) { | ||
@@ -31,6 +32,79 @@ return obj && obj.__esModule ? obj : {default: obj}; | ||
*/ | ||
const isEmptyString = lines => lines.length === 1 && lines[0].length === 0; // Compare two arrays of strings line-by-line. Format as comparison lines. | ||
const isEmptyString = lines => lines.length === 1 && lines[0].length === 0; | ||
const countChanges = diffs => { | ||
let a = 0; | ||
let b = 0; | ||
diffs.forEach(diff => { | ||
switch (diff[0]) { | ||
case _cleanupSemantic.DIFF_DELETE: | ||
a += 1; | ||
break; | ||
case _cleanupSemantic.DIFF_INSERT: | ||
b += 1; | ||
break; | ||
} | ||
}); | ||
return { | ||
a, | ||
b | ||
}; | ||
}; | ||
const printAnnotation = ( | ||
{ | ||
aAnnotation, | ||
aColor, | ||
aIndicator, | ||
bAnnotation, | ||
bColor, | ||
bIndicator, | ||
includeChangeCounts, | ||
omitAnnotationLines | ||
}, | ||
changeCounts | ||
) => { | ||
if (omitAnnotationLines) { | ||
return ''; | ||
} | ||
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 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 baCountLengthDiff = bCount.length - aCount.length; | ||
const aCountPadding = ' '.repeat(Math.max(0, baCountLengthDiff)); | ||
const bCountPadding = ' '.repeat(Math.max(0, -baCountLengthDiff)); | ||
aRest = | ||
aAnnotationPadding + ' ' + aIndicator + ' ' + aCountPadding + aCount; | ||
bRest = | ||
bAnnotationPadding + ' ' + bIndicator + ' ' + bCountPadding + bCount; | ||
} | ||
return ( | ||
aColor(aIndicator + ' ' + aAnnotation + aRest) + | ||
'\n' + | ||
bColor(bIndicator + ' ' + bAnnotation + bRest) + | ||
'\n\n' | ||
); | ||
}; | ||
const printDiffLines = (diffs, options) => | ||
printAnnotation(options, countChanges(diffs)) + | ||
(options.expand | ||
? (0, _joinAlignedDiffs.joinAlignedDiffsExpand)(diffs, options) | ||
: (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) => | ||
(0, _printDiffs.printDiffLines)( | ||
printDiffLines( | ||
diffLinesRaw( | ||
@@ -94,3 +168,3 @@ isEmptyString(aLines) ? [] : aLines, | ||
}); | ||
return (0, _printDiffs.printDiffLines)( | ||
return printDiffLines( | ||
diffs, | ||
@@ -97,0 +171,0 @@ (0, _normalizeDiffOptions.normalizeDiffOptions)(options) |
@@ -124,3 +124,2 @@ 'use strict'; | ||
}; | ||
const FORMAT_OPTIONS_0 = {...FORMAT_OPTIONS, indent: 0}; | ||
const FALLBACK_FORMAT_OPTIONS = { | ||
@@ -130,4 +129,3 @@ callToJSON: false, | ||
plugins: PLUGINS | ||
}; | ||
const FALLBACK_FORMAT_OPTIONS_0 = {...FALLBACK_FORMAT_OPTIONS, indent: 0}; // 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) | ||
@@ -220,44 +218,17 @@ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
let hasThrown = false; | ||
const noDiffMessage = getCommonMessage(_constants.NO_DIFF_MESSAGE, options); | ||
try { | ||
const aCompare = (0, _prettyFormat.format)(a, FORMAT_OPTIONS_0); | ||
const bCompare = (0, _prettyFormat.format)(b, FORMAT_OPTIONS_0); | ||
if (aCompare === bCompare) { | ||
difference = noDiffMessage; | ||
} else { | ||
const aDisplay = (0, _prettyFormat.format)(a, FORMAT_OPTIONS); | ||
const bDisplay = (0, _prettyFormat.format)(b, FORMAT_OPTIONS); | ||
difference = (0, _diffLines.diffLinesUnified2)( | ||
aDisplay.split('\n'), | ||
bDisplay.split('\n'), | ||
aCompare.split('\n'), | ||
bCompare.split('\n'), | ||
options | ||
); | ||
} | ||
const formatOptions = getFormatOptions(FORMAT_OPTIONS, options); | ||
difference = getObjectsDifference(a, b, formatOptions, options); | ||
} catch { | ||
hasThrown = true; | ||
} // 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 aCompare = (0, _prettyFormat.format)(a, FALLBACK_FORMAT_OPTIONS_0); | ||
const bCompare = (0, _prettyFormat.format)(b, FALLBACK_FORMAT_OPTIONS_0); | ||
const formatOptions = getFormatOptions(FALLBACK_FORMAT_OPTIONS, options); | ||
difference = getObjectsDifference(a, b, formatOptions, options); | ||
if (aCompare === bCompare) { | ||
difference = noDiffMessage; | ||
} else { | ||
const aDisplay = (0, _prettyFormat.format)(a, FALLBACK_FORMAT_OPTIONS); | ||
const bDisplay = (0, _prettyFormat.format)(b, FALLBACK_FORMAT_OPTIONS); | ||
difference = (0, _diffLines.diffLinesUnified2)( | ||
aDisplay.split('\n'), | ||
bDisplay.split('\n'), | ||
aCompare.split('\n'), | ||
bCompare.split('\n'), | ||
options | ||
); | ||
} | ||
if (difference !== noDiffMessage && !hasThrown) { | ||
@@ -273,1 +244,28 @@ difference = | ||
} | ||
function getFormatOptions(formatOptions, options) { | ||
const {compareKeys} = (0, _normalizeDiffOptions.normalizeDiffOptions)( | ||
options | ||
); | ||
return {...formatOptions, compareKeys}; | ||
} | ||
function getObjectsDifference(a, b, formatOptions, options) { | ||
const formatOptionsZeroIndent = {...formatOptions, indent: 0}; | ||
const aCompare = (0, _prettyFormat.format)(a, formatOptionsZeroIndent); | ||
const bCompare = (0, _prettyFormat.format)(b, formatOptionsZeroIndent); | ||
if (aCompare === bCompare) { | ||
return getCommonMessage(_constants.NO_DIFF_MESSAGE, options); | ||
} else { | ||
const aDisplay = (0, _prettyFormat.format)(a, formatOptions); | ||
const bDisplay = (0, _prettyFormat.format)(b, formatOptions); | ||
return (0, _diffLines.diffLinesUnified2)( | ||
aDisplay.split('\n'), | ||
bDisplay.split('\n'), | ||
aCompare.split('\n'), | ||
bCompare.split('\n'), | ||
options | ||
); | ||
} | ||
} |
@@ -10,4 +10,2 @@ 'use strict'; | ||
var _printDiffs = require('./printDiffs'); | ||
/** | ||
@@ -19,6 +17,88 @@ * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
*/ | ||
// jest --no-expand | ||
const formatTrailingSpaces = (line, trailingSpaceFormatter) => | ||
line.replace(/\s+$/, match => trailingSpaceFormatter(match)); | ||
const printDiffLine = ( | ||
line, | ||
isFirstOrLast, | ||
color, | ||
indicator, | ||
trailingSpaceFormatter, | ||
emptyFirstOrLastLinePlaceholder | ||
) => | ||
line.length !== 0 | ||
? color( | ||
indicator + ' ' + formatTrailingSpaces(line, trailingSpaceFormatter) | ||
) | ||
: indicator !== ' ' | ||
? color(indicator) | ||
: isFirstOrLast && emptyFirstOrLastLinePlaceholder.length !== 0 | ||
? color(indicator + ' ' + emptyFirstOrLastLinePlaceholder) | ||
: ''; | ||
const printDeleteLine = ( | ||
line, | ||
isFirstOrLast, | ||
{ | ||
aColor, | ||
aIndicator, | ||
changeLineTrailingSpaceColor, | ||
emptyFirstOrLastLinePlaceholder | ||
} | ||
) => | ||
printDiffLine( | ||
line, | ||
isFirstOrLast, | ||
aColor, | ||
aIndicator, | ||
changeLineTrailingSpaceColor, | ||
emptyFirstOrLastLinePlaceholder | ||
); | ||
const printInsertLine = ( | ||
line, | ||
isFirstOrLast, | ||
{ | ||
bColor, | ||
bIndicator, | ||
changeLineTrailingSpaceColor, | ||
emptyFirstOrLastLinePlaceholder | ||
} | ||
) => | ||
printDiffLine( | ||
line, | ||
isFirstOrLast, | ||
bColor, | ||
bIndicator, | ||
changeLineTrailingSpaceColor, | ||
emptyFirstOrLastLinePlaceholder | ||
); | ||
const printCommonLine = ( | ||
line, | ||
isFirstOrLast, | ||
{ | ||
commonColor, | ||
commonIndicator, | ||
commonLineTrailingSpaceColor, | ||
emptyFirstOrLastLinePlaceholder | ||
} | ||
) => | ||
printDiffLine( | ||
line, | ||
isFirstOrLast, | ||
commonColor, | ||
commonIndicator, | ||
commonLineTrailingSpaceColor, | ||
emptyFirstOrLastLinePlaceholder | ||
); // 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 | ||
// | ||
// Given array of aligned strings with inverse highlight formatting, | ||
// return joined lines with diff formatting (and patch marks, if needed). | ||
const joinAlignedDiffsNoExpand = (diffs, options) => { | ||
@@ -98,5 +178,3 @@ const iLength = diffs.length; | ||
const j = lines.length; | ||
lines.push( | ||
(0, _printDiffs.printCommonLine)(line, j === 0 || j === jLast, options) | ||
); | ||
lines.push(printCommonLine(line, j === 0 || j === jLast, options)); | ||
aEnd += 1; | ||
@@ -108,5 +186,3 @@ bEnd += 1; | ||
const j = lines.length; | ||
lines.push( | ||
(0, _printDiffs.printDeleteLine)(line, j === 0 || j === jLast, options) | ||
); | ||
lines.push(printDeleteLine(line, j === 0 || j === jLast, options)); | ||
aEnd += 1; | ||
@@ -117,5 +193,3 @@ }; | ||
const j = lines.length; | ||
lines.push( | ||
(0, _printDiffs.printInsertLine)(line, j === 0 || j === jLast, options) | ||
); | ||
lines.push(printInsertLine(line, j === 0 || j === jLast, options)); | ||
bEnd += 1; | ||
@@ -165,3 +239,3 @@ }; // Second pass: push lines with diff formatting (and patch marks, if needed). | ||
lines[jPatchMark] = (0, _printDiffs.createPatchMark)( | ||
lines[jPatchMark] = createPatchMark( | ||
aStart, | ||
@@ -205,9 +279,3 @@ aEnd, | ||
if (hasPatch) { | ||
lines[jPatchMark] = (0, _printDiffs.createPatchMark)( | ||
aStart, | ||
aEnd, | ||
bStart, | ||
bEnd, | ||
options | ||
); | ||
lines[jPatchMark] = createPatchMark(aStart, aEnd, bStart, bEnd, options); | ||
} | ||
@@ -231,9 +299,9 @@ | ||
case _cleanupSemantic.DIFF_DELETE: | ||
return (0, _printDiffs.printDeleteLine)(line, isFirstOrLast, options); | ||
return printDeleteLine(line, isFirstOrLast, options); | ||
case _cleanupSemantic.DIFF_INSERT: | ||
return (0, _printDiffs.printInsertLine)(line, isFirstOrLast, options); | ||
return printInsertLine(line, isFirstOrLast, options); | ||
default: | ||
return (0, _printDiffs.printCommonLine)(line, isFirstOrLast, options); | ||
return printCommonLine(line, isFirstOrLast, options); | ||
} | ||
@@ -240,0 +308,0 @@ }) |
@@ -36,2 +36,3 @@ 'use strict'; | ||
commonLineTrailingSpaceColor: noColor, | ||
compareKeys: undefined, | ||
contextLines: DIFF_CONTEXT_DEFAULT, | ||
@@ -45,2 +46,7 @@ emptyFirstOrLastLinePlaceholder: '', | ||
const getCompareKeys = compareKeys => | ||
compareKeys && typeof compareKeys === 'function' | ||
? compareKeys | ||
: OPTIONS_DEFAULT.compareKeys; | ||
const getContextLines = contextLines => | ||
@@ -56,2 +62,3 @@ typeof contextLines === 'number' && | ||
...options, | ||
compareKeys: getCompareKeys(options.compareKeys), | ||
contextLines: getContextLines(options.contextLines) | ||
@@ -58,0 +65,0 @@ }); |
@@ -8,16 +8,4 @@ /** | ||
import { Diff } from './cleanupSemantic'; | ||
import type { DiffOptions, DiffOptionsNormalized } from './types'; | ||
export declare const printDeleteLine: (line: string, isFirstOrLast: boolean, { aColor, aIndicator, changeLineTrailingSpaceColor, emptyFirstOrLastLinePlaceholder, }: DiffOptionsNormalized) => string; | ||
export declare const printInsertLine: (line: string, isFirstOrLast: boolean, { bColor, bIndicator, changeLineTrailingSpaceColor, emptyFirstOrLastLinePlaceholder, }: DiffOptionsNormalized) => string; | ||
export declare const printCommonLine: (line: string, isFirstOrLast: boolean, { commonColor, commonIndicator, commonLineTrailingSpaceColor, emptyFirstOrLastLinePlaceholder, }: DiffOptionsNormalized) => string; | ||
export declare const hasCommonDiff: (diffs: Array<Diff>, isMultiline: boolean) => boolean; | ||
export declare type ChangeCounts = { | ||
a: number; | ||
b: number; | ||
}; | ||
export declare const countChanges: (diffs: Array<Diff>) => ChangeCounts; | ||
export declare const printAnnotation: ({ aAnnotation, aColor, aIndicator, bAnnotation, bColor, bIndicator, includeChangeCounts, omitAnnotationLines, }: DiffOptionsNormalized, changeCounts: ChangeCounts) => string; | ||
export declare const printDiffLines: (diffs: Array<Diff>, options: DiffOptionsNormalized) => string; | ||
export declare const createPatchMark: (aStart: number, aEnd: number, bStart: number, bEnd: number, { patchColor }: DiffOptionsNormalized) => string; | ||
import type { DiffOptions } from './types'; | ||
export declare const diffStringsUnified: (a: string, b: string, options?: DiffOptions | undefined) => string; | ||
export declare const diffStringsRaw: (a: string, b: string, cleanup: boolean) => Array<Diff>; |
@@ -6,13 +6,3 @@ 'use strict'; | ||
}); | ||
exports.diffStringsRaw = | ||
exports.diffStringsUnified = | ||
exports.createPatchMark = | ||
exports.printDiffLines = | ||
exports.printAnnotation = | ||
exports.countChanges = | ||
exports.hasCommonDiff = | ||
exports.printCommonLine = | ||
exports.printInsertLine = | ||
exports.printDeleteLine = | ||
void 0; | ||
exports.diffStringsRaw = exports.diffStringsUnified = void 0; | ||
@@ -27,4 +17,2 @@ var _cleanupSemantic = require('./cleanupSemantic'); | ||
var _joinAlignedDiffs = require('./joinAlignedDiffs'); | ||
var _normalizeDiffOptions = require('./normalizeDiffOptions'); | ||
@@ -42,86 +30,2 @@ | ||
*/ | ||
const formatTrailingSpaces = (line, trailingSpaceFormatter) => | ||
line.replace(/\s+$/, match => trailingSpaceFormatter(match)); | ||
const printDiffLine = ( | ||
line, | ||
isFirstOrLast, | ||
color, | ||
indicator, | ||
trailingSpaceFormatter, | ||
emptyFirstOrLastLinePlaceholder | ||
) => | ||
line.length !== 0 | ||
? color( | ||
indicator + ' ' + formatTrailingSpaces(line, trailingSpaceFormatter) | ||
) | ||
: indicator !== ' ' | ||
? color(indicator) | ||
: isFirstOrLast && emptyFirstOrLastLinePlaceholder.length !== 0 | ||
? color(indicator + ' ' + emptyFirstOrLastLinePlaceholder) | ||
: ''; | ||
const printDeleteLine = ( | ||
line, | ||
isFirstOrLast, | ||
{ | ||
aColor, | ||
aIndicator, | ||
changeLineTrailingSpaceColor, | ||
emptyFirstOrLastLinePlaceholder | ||
} | ||
) => | ||
printDiffLine( | ||
line, | ||
isFirstOrLast, | ||
aColor, | ||
aIndicator, | ||
changeLineTrailingSpaceColor, | ||
emptyFirstOrLastLinePlaceholder | ||
); | ||
exports.printDeleteLine = printDeleteLine; | ||
const printInsertLine = ( | ||
line, | ||
isFirstOrLast, | ||
{ | ||
bColor, | ||
bIndicator, | ||
changeLineTrailingSpaceColor, | ||
emptyFirstOrLastLinePlaceholder | ||
} | ||
) => | ||
printDiffLine( | ||
line, | ||
isFirstOrLast, | ||
bColor, | ||
bIndicator, | ||
changeLineTrailingSpaceColor, | ||
emptyFirstOrLastLinePlaceholder | ||
); | ||
exports.printInsertLine = printInsertLine; | ||
const printCommonLine = ( | ||
line, | ||
isFirstOrLast, | ||
{ | ||
commonColor, | ||
commonIndicator, | ||
commonLineTrailingSpaceColor, | ||
emptyFirstOrLastLinePlaceholder | ||
} | ||
) => | ||
printDiffLine( | ||
line, | ||
isFirstOrLast, | ||
commonColor, | ||
commonIndicator, | ||
commonLineTrailingSpaceColor, | ||
emptyFirstOrLastLinePlaceholder | ||
); | ||
exports.printCommonLine = printCommonLine; | ||
const hasCommonDiff = (diffs, isMultiline) => { | ||
@@ -139,91 +43,5 @@ if (isMultiline) { | ||
return diffs.some(diff => diff[0] === _cleanupSemantic.DIFF_EQUAL); | ||
}; | ||
exports.hasCommonDiff = hasCommonDiff; | ||
const countChanges = diffs => { | ||
let a = 0; | ||
let b = 0; | ||
diffs.forEach(diff => { | ||
switch (diff[0]) { | ||
case _cleanupSemantic.DIFF_DELETE: | ||
a += 1; | ||
break; | ||
case _cleanupSemantic.DIFF_INSERT: | ||
b += 1; | ||
break; | ||
} | ||
}); | ||
return { | ||
a, | ||
b | ||
}; | ||
}; | ||
exports.countChanges = countChanges; | ||
const printAnnotation = ( | ||
{ | ||
aAnnotation, | ||
aColor, | ||
aIndicator, | ||
bAnnotation, | ||
bColor, | ||
bIndicator, | ||
includeChangeCounts, | ||
omitAnnotationLines | ||
}, | ||
changeCounts | ||
) => { | ||
if (omitAnnotationLines) { | ||
return ''; | ||
} | ||
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 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 baCountLengthDiff = bCount.length - aCount.length; | ||
const aCountPadding = ' '.repeat(Math.max(0, baCountLengthDiff)); | ||
const bCountPadding = ' '.repeat(Math.max(0, -baCountLengthDiff)); | ||
aRest = | ||
aAnnotationPadding + ' ' + aIndicator + ' ' + aCountPadding + aCount; | ||
bRest = | ||
bAnnotationPadding + ' ' + bIndicator + ' ' + bCountPadding + bCount; | ||
} | ||
return ( | ||
aColor(aIndicator + ' ' + aAnnotation + aRest) + | ||
'\n' + | ||
bColor(bIndicator + ' ' + bAnnotation + bRest) + | ||
'\n\n' | ||
); | ||
}; | ||
exports.printAnnotation = printAnnotation; | ||
const printDiffLines = (diffs, options) => | ||
printAnnotation(options, countChanges(diffs)) + | ||
(options.expand | ||
? (0, _joinAlignedDiffs.joinAlignedDiffsExpand)(diffs, options) | ||
: (0, _joinAlignedDiffs.joinAlignedDiffsNoExpand)(diffs, options)); // In GNU diff format, indexes are one-based instead of zero-based. | ||
exports.printDiffLines = printDiffLines; | ||
const createPatchMark = (aStart, aEnd, bStart, bEnd, {patchColor}) => | ||
patchColor( | ||
`@@ -${aStart + 1},${aEnd - aStart} +${bStart + 1},${bEnd - bStart} @@` | ||
); // Compare two strings character-by-character. | ||
}; // Compare two strings character-by-character. | ||
// Format as comparison lines in which changed substrings have inverse colors. | ||
exports.createPatchMark = createPatchMark; | ||
const diffStringsUnified = (a, b, options) => { | ||
@@ -247,3 +65,3 @@ if (a !== b && a.length !== 0 && b.length !== 0) { | ||
); | ||
return printDiffLines(lines, optionsNormalized); | ||
return (0, _diffLines.printDiffLines)(lines, optionsNormalized); | ||
} | ||
@@ -250,0 +68,0 @@ } // Fall back to line-by-line diff. |
@@ -7,2 +7,3 @@ /** | ||
*/ | ||
import type { CompareKeys } from 'pretty-format'; | ||
export declare type DiffOptionsColor = (arg: string) => string; | ||
@@ -27,2 +28,3 @@ export declare type DiffOptions = { | ||
patchColor?: DiffOptionsColor; | ||
compareKeys?: CompareKeys; | ||
}; | ||
@@ -41,2 +43,3 @@ export declare type DiffOptionsNormalized = { | ||
commonLineTrailingSpaceColor: DiffOptionsColor; | ||
compareKeys: CompareKeys; | ||
contextLines: number; | ||
@@ -43,0 +46,0 @@ emptyFirstOrLastLinePlaceholder: string; |
{ | ||
"name": "jest-diff", | ||
"version": "27.3.1", | ||
"version": "27.4.0", | ||
"repository": { | ||
@@ -13,3 +13,6 @@ "type": "git", | ||
"exports": { | ||
".": "./build/index.js", | ||
".": { | ||
"types": "./build/index.d.ts", | ||
"default": "./build/index.js" | ||
}, | ||
"./package.json": "./package.json" | ||
@@ -19,8 +22,8 @@ }, | ||
"chalk": "^4.0.0", | ||
"diff-sequences": "^27.0.6", | ||
"jest-get-type": "^27.3.1", | ||
"pretty-format": "^27.3.1" | ||
"diff-sequences": "^27.4.0", | ||
"jest-get-type": "^27.4.0", | ||
"pretty-format": "^27.4.0" | ||
}, | ||
"devDependencies": { | ||
"@jest/test-utils": "^27.3.1", | ||
"@jest/test-utils": "^27.4.0", | ||
"strip-ansi": "^6.0.0" | ||
@@ -34,3 +37,3 @@ }, | ||
}, | ||
"gitHead": "4f3328f3227aa0668486f819b3353af5b6cc797b" | ||
"gitHead": "0dc6dde296550370ade2574d6665748fed37f9c9" | ||
} |
@@ -397,2 +397,3 @@ # jest-diff | ||
| `commonLineTrailingSpaceColor` | `string => string` | | ||
| `compareKeys` | `undefined` | | ||
| `contextLines` | `5` | | ||
@@ -616,1 +617,57 @@ | `emptyFirstOrLastLinePlaceholder` | `''` | | ||
| `commonIndicator` | `' ·'` | `''` | | ||
### Example of option for sorting object keys | ||
When two objects are compared their keys are printed in alphabetical order by default. If this was not the original order of the keys the diff becomes harder to read as the keys are not in their original position. | ||
Use `compareKeys` to pass a function which will be used when sorting the object keys. | ||
```js | ||
const a = {c: 'c', b: 'b1', a: 'a'}; | ||
const b = {c: 'c', b: 'b2', a: 'a'}; | ||
const options = { | ||
// The keys will be in their original order | ||
compareKeys: () => 0, | ||
}; | ||
const difference = diff(a, b, options); | ||
``` | ||
```diff | ||
- Expected | ||
+ Received | ||
Object { | ||
"c": "c", | ||
- "b": "b1", | ||
+ "b": "b2", | ||
"a": "a", | ||
} | ||
``` | ||
Depending on the implementation of `compareKeys` any sort order can be used. | ||
```js | ||
const a = {c: 'c', b: 'b1', a: 'a'}; | ||
const b = {c: 'c', b: 'b2', a: 'a'}; | ||
const options = { | ||
// The keys will be in reverse order | ||
compareKeys: (a, b) => (a > b ? -1 : 1), | ||
}; | ||
const difference = diff(a, b, options); | ||
``` | ||
```diff | ||
- Expected | ||
+ Received | ||
Object { | ||
"a": "a", | ||
- "b": "b1", | ||
+ "b": "b2", | ||
"c": "c", | ||
} | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
672
85165
1833
Updateddiff-sequences@^27.4.0
Updatedjest-get-type@^27.4.0
Updatedpretty-format@^27.4.0