object-diff-lite
v1.0.1
Published
A lightweight package to diff two objects and return old and new values for change history.
Maintainers
Readme
object-diff-lite 🛠️
Lightweight JavaScript utility to diff two objects and get old vs. new values for history tracking.
Features ✨
- Compare two objects and return changes with
oldValueandnewValue - Supports nested objects
- Optional
prefixKeyfor namespacing keys - Optional
ignoreKeysarray to skip certain keys
Installation 💾
npm install object-diff-liteFunction Signature 📌
diffObjects(oldObj, newObj, (prefixKey = ""), (ignoreKeys = []));Import / Usage 📥
Node.js (Common JS)
const diffObjects = require("object-diff-lite");React / Browser (ES Modules)
import diffObjects from "object-diff-lite";Example 💡
const oldObj = {
name: "David",
age: 30,
profile: { email: "[email protected]", city: "Berlin" },
};
const newObj = {
name: "David",
age: 31,
profile: { email: "[email protected]", city: "Wolfsburg" },
};
// Basic diff
console.log(diffObjects(oldObj, newObj));
// With prefixKey
console.log(diffObjects(oldObj, newObj, "user"));
// Ignoring specific keys
console.log(diffObjects(oldObj, newObj, "", ["age", "profile.city"]));Output 🖥️
Basic diff:
[
{ key: 'age', oldValue: 30, newValue: 31 },
{ key: 'profile.email', oldValue: '[email protected]', newValue: '[email protected]' },
{ key: 'profile.city', oldValue: 'Berlin', newValue: 'Wolfsburg' }
]
With prefixKey:
[
{ key: 'user.age', oldValue: 30, newValue: 31 },
{ key: 'user.profile.email', oldValue: '[email protected]', newValue: '[email protected]' },
{ key: 'user.profile.city', oldValue: 'Berlin', newValue: 'Wolfsburg' }
]
Ignoring keys:
[
{ key: 'profile.email', oldValue: '[email protected]', newValue: '[email protected]' }
]