Skip to content

πŸ“¦ Lightweight TypeScript utility for comparing objects, arrays, and nested data structures.

Notifications You must be signed in to change notification settings

jaktestowac/data-diff-ts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

data-diff-ts

Lightweight TypeScript utility for comparing objects and arrays.

License: MIT

✨ Features

  • πŸ”Ž Deep diff between objects
  • βœ… Detects added, removed, changed, unchanged
  • 🧠 Designed for testing, debugging, automation
  • 🌱 Zero dependencies

πŸ“¦ Install

npm install data-diff-ts

πŸ“¦ Usage

Basic Object Diff

import { diff } from "data-diff-ts";

const a = { name: "Alice", age: 25 };
const b = { name: "Alicia", age: 25 };

const result = diff(a, b);

console.log(result);
/*
{
  added: {},
  removed: {},
  changed: {
    name: { from: 'Alice', to: 'Alicia' }
  },
  unchanged: {
    age: 25
  }
}
*/

Flattened Output (dot notation)

import { smartDiff } from "data-diff-ts";

const a = { user: { name: "Alice", email: "alice@example.com" } };
const b = { user: { name: "Alicia", email: "alice@example.com" } };

const flat = smartDiff(a, b, { flatten: true });

console.log(flat);
/*
{
  "user.name": { from: "Alice", to: "Alicia" }
}
*/

// Without flattening
const nested = smartDiff(a, b);

console.log(nested);
/*
{
  added: {},
  changed: {
    user: {
      added: {},
      changed: {
        name: {
          from: "Alice",
          to: "Alicia",
        },
      },
      removed: {},
      unchanged: {
        email: "alice@example.com",
      },
    },
  },
  removed: {},
  unchanged: {},
}
*/

Compare Arrays

import { diffArrays } from "data-diff-ts";

const a = ["a", "b", "c"];
const b = ["a", "x", "c", "d"];

const result = diffArrays(a, b);

console.log(result);
/*
{
  added: ['d'],
  removed: [],
  changed: [
    { index: 1, from: 'b', to: 'x' }
  ],
  unchanged: ['a', 'c']
}
*/

Deeply Nested Structure

import { smartDiff } from "data-diff-ts";

const a = {
  user: {
    name: "Alice",
    address: {
      city: "London",
      zip: "12345",
    },
  },
};

const b = {
  user: {
    name: "Alicia",
    address: {
      city: "Paris",
      zip: "54321",
    },
  },
};

const result = smartDiff(a, b, { flatten: true });

console.log(result);
/*
{
  "user.name": { from: "Alice", to: "Alicia" },
  "user.address.city": { from: "London", to: "Paris" },
  "user.address.zip": { from: "12345", to: "54321" }
}
*/

Detect Object Differences in Arrays

const a = {
  users: [
    { name: "Alice", active: true },
    { name: "Bob", active: false },
  ],
};

const b = {
  users: [
    { name: "Alice", active: true },
    { name: "Bob", active: true },
  ],
};

const result = smartDiff(a, b, { flatten: true });

console.log(result);
/*
{
  "users[1].active": { from: false, to: true }
}
*/

πŸ“„ License

MIT Β© jaktestowac.pl

Powered by jaktestowac.pl team!

🌐 Check out GitHub profile for more open-source projects and resources.

About

πŸ“¦ Lightweight TypeScript utility for comparing objects, arrays, and nested data structures.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published