Display differences clearly so people can review changes confidently
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Character-by-character string comparison with unified diff output, semantic cleanup, and support for both single-line and multi-line strings.
Compares two strings character-by-character and returns formatted unified diff output.
/**
* Compare two strings character-by-character and format as comparison lines
* in which changed substrings have inverse colors
* @param a - First string to compare
* @param b - Second string to compare
* @param options - Optional formatting configuration
* @returns Formatted unified diff string
*/
function diffStringsUnified(a: string, b: string, options?: DiffOptions): string;Features:
Usage Examples:
import { diffStringsUnified } from "jest-diff";
// Single-line strings
const result1 = diffStringsUnified("Hello world", "Hello Jest");
console.log(result1);
// Multi-line strings
const text1 = `Line 1
Line 2
Line 3`;
const text2 = `Line 1
Modified Line 2
Line 3`;
const result2 = diffStringsUnified(text1, text2);
console.log(result2);
// With custom options
const result3 = diffStringsUnified("old text", "new text", {
aAnnotation: "Original",
bAnnotation: "Updated",
contextLines: 2
});
console.log(result3);Compares two strings character-by-character and returns raw diff operations array.
/**
* Compare two strings character-by-character and return raw diff operations
* @param a - First string to compare
* @param b - Second string to compare
* @param cleanup - Whether to apply semantic cleanup to improve diff quality
* @returns Array of Diff objects representing the operations
*/
function diffStringsRaw(a: string, b: string, cleanup: boolean): Array<Diff>;Cleanup Process:
Usage Examples:
import { diffStringsRaw, DIFF_DELETE, DIFF_INSERT, DIFF_EQUAL } from "jest-diff";
// Basic usage with cleanup
const diffs = diffStringsRaw("Hello world", "Hello Jest", true);
diffs.forEach(diff => {
const [op, text] = [diff[0], diff[1]];
switch (op) {
case DIFF_DELETE:
console.log(`Delete: "${text}"`);
break;
case DIFF_INSERT:
console.log(`Insert: "${text}"`);
break;
case DIFF_EQUAL:
console.log(`Keep: "${text}"`);
break;
}
});
// Without cleanup for raw operations
const rawDiffs = diffStringsRaw("abc", "axc", false);
console.log(rawDiffs);class Diff {
0: number; // Operation type
1: string; // Text content
constructor(op: number, text: string);
}
const DIFF_DELETE = -1; // Text was deleted from first string
const DIFF_INSERT = 1; // Text was inserted in second string
const DIFF_EQUAL = 0; // Text is common to both strings