This NPM compares two JSON objects and return changes between them.
This is a great way to get a summary of changes for one object before save it on database or just to show differences in order to make a decision.
# from NPM
npm i easy-diff-mapper
or
# from Github
npm i git+https://github.com/ariassd/easy-diff-mapper.git
EasyDiffMapper.compare
import { EasyDiffMapper } from './easy-diff-mapper';
// ...
// declare the original object
const obj1 = {
name: 'Ed Edwards',
address: 'St 10, Av 10, Building 1023',
country: 'Costa Rica',
contactData: [
{
type: 'email',
description: 'personal',
contact: '[email protected]',
},
{
type: 'email',
description: 'work',
contact: '[email protected]',
},
],
pendingTasks: ['Prepare invoice payments', 'Update excel payroll information', 'Make a deposit for payroll'],
department: {
name: 'finances',
offices: ['408 4th floor, finances main office', '103 1st floor, finances reception'],
},
};
// make a copy of the original object and do some changes.
const copyObj1 = JSON.parse(JSON.stringify(obj1));
delete copyObj1.country;
copyObj1.birthday = '1990-08-21';
copyObj1.address = 'St 10, Av 33, Building 1023';
copyObj1.contactData.pop();
copyObj1.contactData.push({
type: 'phone',
description: 'personal',
contact: '+(506)8978675645',
});
copyObj1.contactData.push({
type: 'fax',
description: 'work',
contact: '+(506)8778',
});
copyObj1.pendingTasks.pop();
copyObj1.department.offices[1] = '103 1st floor, finances reception 👈 here is the money 🤫';
copyObj1.department.name = 'Finances';
// Let's compare both objects
const edf = new EasyDiffMapper();
const diff = await edf.compare(obj1, copyObj1, {
excludeUnchanged: false,
});
console.log(JSON.stringify(diff, null, ' '));Result json
{
"name": {
"changeType": "Unchanged",
"old": "Ed Edwards",
"new": "Ed Edwards"
},
"address": {
"changeType": "Updated",
"old": "St 10, Av 10, Building 1023",
"new": "St 10, Av 33, Building 1023"
},
"country": {
"changeType": "Deleted",
"old": "Costa Rica"
},
"contactData": {
"0": {
"type": {
"changeType": "Unchanged",
"old": "email",
"new": "email"
},
"description": {
"changeType": "Unchanged",
"old": "personal",
"new": "personal"
},
"contact": {
"changeType": "Unchanged",
"old": "[email protected]",
"new": "[email protected]"
}
},
"1": {
"type": {
"changeType": "Updated",
"old": "email",
"new": "phone"
},
"description": {
"changeType": "Updated",
"old": "work",
"new": "personal"
},
"contact": {
"changeType": "Updated",
"old": "[email protected]",
"new": "+(506)8978675645"
}
}
},
"pendingTasks": {
"0": {
"changeType": "Unchanged",
"old": "Prepare invoice payments",
"new": "Prepare invoice payments"
},
"1": {
"changeType": "Unchanged",
"old": "Update excel payroll information",
"new": "Update excel payroll information"
},
"2": {
"changeType": "Deleted",
"old": "Make a deposit for payroll"
}
},
"department": {
"name": {
"changeType": "Updated",
"old": "finances",
"new": "Finances"
},
"offices": {
"0": {
"changeType": "Unchanged",
"old": "408 4th floor, finances main office",
"new": "408 4th floor, finances main office"
},
"1": {
"changeType": "Updated",
"old": "103 1st floor, finances reception",
"new": "103 1st floor, finances reception 👈 here is the money 🤫"
}
}
}
}| Parameter | type | description |
|---|---|---|
| originalObject | any | Object without changes |
| newObject | any | Updated object |
| options | Options | |
| options.excludeUnchanged | boolean | True to exclude fields without changes |
EasyDiffMapper used as a instanced class provide the following methods.
const diff = new EasyDiffMapper();
await diff.compare(obj1, obj2, options);- Author - Luis Arias 2021 <[email protected]> GitHub profile
This software is licensed under MIT License
November 2021


