Concordance JavaScript Value Utility

5.0.4 · active · verified Sun Apr 19

Concordance is a robust JavaScript utility library designed for deep comparison, human-readable formatting, detailed diffing, and consistent serialization of any JavaScript value. Currently stable at version 5.0.4, it maintains an active release cadence with frequent patch releases addressing minor issues and less frequent major versions that introduce breaking changes or significant feature enhancements. A key differentiator is its consistent underlying algorithm applied across all core operations—comparison, formatting, and diffing—ensuring predictable behavior. It offers granular control over comparisons, notably treating `-0` as distinct from `0`, `NaN` as equal to `NaN`, and comparing functions, promises, and symbols strictly by identity. Formatting is optimized for legibility, including special handling for multi-line strings and control characters. Concordance also supports serialization for later comparison, with specific behavioral changes noted when a deserialized value is used as the 'actual' value in comparisons.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `diff` to compare two complex JavaScript objects and `format` to render the differences in a human-readable way. It also includes an example highlighting Concordance's unique handling of `-0` versus `0`.

import { diff, format } from 'concordance';

interface User {
  id: number;
  name: string;
  email?: string;
  address: {
    street: string;
    city: string;
    zip: string;
  };
  roles: string[];
  createdAt: Date;
  lastLogin?: Date;
  preferences?: Map<string, any>;
}

const user1: User = {
  id: 1,
  name: 'Alice',
  email: 'alice@example.com',
  address: {
    street: '123 Main St',
    city: 'Anytown',
    zip: '12345'
  },
  roles: ['admin', 'editor'],
  createdAt: new Date('2023-01-01T10:00:00Z'),
  preferences: new Map([['theme', 'dark'], ['notifications', true]])
};

const user2: User = {
  id: 1,
  name: 'Alicia', // Changed name
  // email missing
  address: {
    street: '123 Main Street', // Street name changed slightly
    city: 'Anytown',
    zip: '12345'
  },
  roles: ['editor'], // Role removed
  createdAt: new Date('2023-01-01T10:00:00Z'),
  lastLogin: new Date('2024-04-18T15:30:00Z') // New field
};

console.log("--- Diffing two user objects ---");
const userDiff = diff(user1, user2);
console.log(format(userDiff));

const objA = { a: 1, b: { c: 2 } };
const objB = { a: 1, b: { c: 3, d: 4 } };
console.log("\n--- Diffing simple objects ---");
console.log(format(diff(objA, objB)));

// Example of how -0 and 0 are handled distinctly
console.log("\n--- Comparing -0 and 0 ---");
const negativeZeroDiff = diff(-0, 0);
console.log(format(negativeZeroDiff)); // Will show a difference

view raw JSON →