A modern TypeScript/Deno implementation of the deep object difference algorithm, providing utilities for determining structural differences between objects and applying those differences.
This is a complete rewrite of the original deep-diff library, modernized for TypeScript and Deno while maintaining full compatibility with the legacy API.
- π Deep comparison - Find structural differences between objects
- π Observable differences - React to changes as they're discovered
- π Apply changes - Transform objects by applying differences
- π― Selective application - Apply only specific changes with filters
- π Order-independent comparison - Compare arrays regardless of element order
- π Type-safe - Full TypeScript support with comprehensive type definitions
- π Modern runtime - Built for Deno with ES modules
import deepDiff from "jsr:@fry69/deep-diff";npx jsr add @fry69/deep-diffimport deepDiff from "@fry69/deep-diff";import deepDiff from "jsr:@fry69/deep-diff";
const oldObj = {
name: "John",
age: 30,
hobbies: ["reading", "gaming"],
};
const newObj = {
name: "John",
age: 31,
hobbies: ["reading", "gaming", "cooking"],
city: "New York",
};
// Find differences
const differences = deepDiff(oldObj, newObj);
console.log(differences);
/*
[
{ kind: "E", path: ["age"], lhs: 30, rhs: 31 },
{ kind: "A", path: ["hobbies"], index: 2, item: { kind: "N", rhs: "cooking" } },
{ kind: "N", path: ["city"], rhs: "New York" }
]
*/import deepDiff from "jsr:@fry69/deep-diff";
const source = { name: "Alice", score: 100 };
const target = { name: "Alice", score: 95, level: 1 };
// Apply all differences from target to source
deepDiff.applyDiff(source, target);
console.log(source); // { name: "Alice", score: 95, level: 1 }
// Or apply individual changes
const changes = deepDiff(source, target);
changes?.forEach((change) => {
deepDiff.applyChange(source, target, change);
});Compare two objects and return their differences.
lhs- Left-hand side object (original)rhs- Right-hand side object (comparison)prefilter- Optional function to filter which properties to compareaccumulator- Optional array to collect differences
Returns an array of differences or undefined if objects are identical.
Compare objects and call observer function for each difference found.
Apply a single change to the target object.
Apply all differences from source to target, optionally filtered.
Compare objects treating arrays as order-independent sets.
Each difference has a kind property indicating the type of change:
N(New) - A property was addedD(Deleted) - A property was removedE(Edited) - A property value changedA(Array) - An array element changed
// Only compare specific properties
const prefilter = (path: any[], key: any) => key !== "timestamp";
const diff = deepDiff(obj1, obj2, prefilter);
// Apply only certain types of changes
const filter = (target, source, change) => change.kind !== "D"; // Skip deletions
deepDiff.applyDiff(target, source, filter);const arr1 = [{ id: 1 }, { id: 2 }, { id: 3 }];
const arr2 = [{ id: 3 }, { id: 1 }, { id: 2 }]; // Same elements, different order
const normalDiff = deepDiff(arr1, arr2); // Shows differences
const orderIndepDiff = deepDiff.orderIndependentDiff(arr1, arr2); // undefined (no differences)This implementation maintains full API compatibility with the original deep-diff library. Simply replace your import and the code should work identically:
// Before
const diff = require("deep-diff");
// After
import deepDiff from "jsr:@fry69/deep-diff";
const diff = deepDiff; // Full compatibilityMIT
This is a modern TypeScript rewrite of the original deep-diff library by Phillip Clark. The original algorithm and behavior have been faithfully preserved while adding TypeScript support and modernizing the codebase for current JavaScript runtimes.