CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-jest-diff

Display differences clearly so people can review changes confidently

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

string-comparison.mddocs/

String Comparison

Character-by-character string comparison with unified diff output, semantic cleanup, and support for both single-line and multi-line strings.

Capabilities

Unified String Diff

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:

  • Multiline Detection: Automatically detects multiline strings and appends newlines for proper alignment
  • Character-level Highlighting: Shows exact character differences with color highlighting
  • Common Substring Detection: Identifies and preserves common substrings for readability
  • Fallback Handling: Falls back to line-by-line diff when character diff isn't meaningful

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);

Raw String Diff

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:

  • Semantic Cleanup: When enabled, eliminates semantically trivial equalities and improves diff readability
  • Overlap Detection: Finds overlaps between deletions and insertions to reduce noise
  • Boundary Alignment: Aligns edits to word and line boundaries where possible

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);

Diff Operations

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

docs

index.md

line-comparison.md

string-comparison.md

value-comparison.md

tile.json