Visual Variable Diff

2.0.2 · active · verified Tue Apr 21

variable-diff is a JavaScript/TypeScript library designed to generate a visual diff between two JavaScript variables, primarily objects or JSON structures. It focuses on presenting only the differences (additions, deletions, and modifications) in a human-readable, formatted output, making it easier to identify changes without sifting through identical data. The current stable version is 2.0.2. While there isn't an explicit release cadence, the project appears to be actively maintained, with recent updates introducing new features like an options argument in version 2.0.1. Its key differentiator is its emphasis on clear, colored, and indented output directly within the console or logs, making it particularly useful for debugging and comparing configuration objects or data states efficiently.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates the basic usage of `variable-diff` to visually compare two TypeScript objects (simulated user profiles), highlighting changes with custom indentation and color options, and also showing how to check if changes were detected programmatically.

import diff from 'variable-diff';

interface UserProfile {
  id: string;
  name: string;
  email: string;
  preferences?: {
    theme: 'dark' | 'light';
    notifications: boolean;
    language: string;
  };
  roles: string[];
}

const oldProfile: UserProfile = {
  id: 'user_123',
  name: 'Alice Smith',
  email: 'alice@example.com',
  preferences: {
    theme: 'dark',
    notifications: true,
    language: 'en-US'
  },
  roles: ['user', 'editor']
};

const newProfile: UserProfile = {
  id: 'user_123',
  name: 'Alice Johnson',
  email: 'alice.johnson@example.com',
  preferences: {
    theme: 'light',
    notifications: false,
    language: 'en-GB'
  },
  roles: ['user', 'admin']
};

const diffOptions = {
  indent: '  ', // 2 spaces for indentation
  newLine: '\n', // Newline character
  color: true // Enable colored output, set to false for plain text
};

console.log('--- Profile Changes ---\n');
const result = diff(oldProfile, newProfile, diffOptions);
console.log(result.text);

if (result.changed) {
  console.log('\nDetected changes in profile data.');
} else {
  console.log('\nNo changes detected in profile data.');
}

// Accessing programmatic changes (raw object)
// console.log('\nRaw changes object:', JSON.stringify(result.changes, null, 2));

view raw JSON →