This library provide a function to get deep difference between two javascript objects.
It's main focus is to highlight differences between two objects, before and after saving to a database, to administer a database change log.
This project is a fork of yyk222/object-difference-js. This version changes the project in a few ways for the City's use case.
- Converts the project to Typescript.
- Updates the code with more current Javascript functionality.
- Simplifies the comparisons for our use case.
npm install @cityssm/object-differenceimport getObjectDifference from '@cityssm/object-difference'
const personBefore = {
firstName: 'John',
age: 30,
gender: 'm',
address: {
city: 'New York',
country: 'USA'
}
}
const personAfter = {
firstName: 'John',
lastName: 'Doe',
age: 31,
address: {
city: 'Los Angeles',
country: 'USA'
}
}
const differences = getObjectDifference(personBefore, personAfter)
console.log(differences)
/*
[
{ property: 'age', type: 'updated', from: 30, to: 31 },
{ property: 'gender', type: 'deleted', from: 'm', to: undefined },
{
property: 'address.city',
type: 'updated',
from: 'New York',
to: 'Los Angeles'
},
{ property: 'lastName', type: 'created', from: undefined, to: 'Doe' }
]
*/Property names are chained by "." between levels. Type can have 4 values:
- updated - Property is present in both objects, but changed.
- created - Property is blank in the first object, and present in the second.
- deleted - Property is present in the first object, and blank in the second.
- NA - Properties cannot be compared (i.e. functions)
First types are checked. Unlike the project this is based off, strings and numbers are not considered the same, and a change in type will be flagged as a difference.
Next, simple values are compared (i.e. boolean, number, and string).
Next, Date objects are compared using the getTime() function.
Finally, arrays are compared. Order does not matter, just the elements.
Sunrise CMS
A completely free, open source, web-based application to assist cemetery managers
with managing their cemetery records.