Compares two objects and returns the differences between them in different formats: changed values, changed paths, differences.
Works in Node.js and in the browser.
$ pnpm i o2diff
import { diff, diffPaths, diffValues } from 'o2diff'
const original = {
firstName: 'John',
lastName: 'Smith',
email: 'john@mail.com',
phones: [
{ type: 'home', value: '+12222' },
{ type: 'mobile', value: '+11111' }
]
}
const current = {
firstName: 'Michael',
age: 25,
email: 'michael@mail.com',
phones: [
{ type: 'work', value: '+13333' },
{ type: 'mobile', value: '+11111' }
],
address: {
city: 'New York',
location: {
latitude: 40.730610,
longitude: -73.935242
}
}
}
// objects diff
diff(original, current)
/*
{
left: {
firstName: 'John',
lastName: 'Smith',
email: 'john@mail.com',
phones: [{ type: 'home', value: '+12222' }]
},
right: {
firstName: 'Michael',
age: 25,
email: 'michael@mail.com',
phones: [{ type: 'work', value: '+13333' }],
address: {
city: 'New York',
location: { latitude: 40.730610, longitude: -73.935242 }
}
}
}
*/
// values diff
diffValues(original, current)
/*
{
changed: {
firstName: 'Michael',
email: 'michael@mail.com',
phones: [{ type: 'home', value: '+12222' }]
},
added: {
age: 25,
address: {
city: 'New York',
location: { latitude: 40.730610, longitude: -73.935242 }
}
},
deleted: {
lastName: 'Smith'
}
}
*/
// paths diff
diffPaths(original, current)
/*
{
changed: [
'firstName',
'email',
'phones[0].type',
'phones[0].value'
],
added: [
'age',
'address.city',
'address.location.latitude',
'address.location.longitude'
],
deleted: [
'lastName'
]
}
*/
Returns the difference between original
and current
.
original: Input
- the original object.current: Input
- the current (actual) object.- returns
{ left, right }: DiffResult
object.
Returns added, changed and deleted values between original
and current
.
original: Input
- the original object.current: Input
- the current (actual) object.- returns
{ changed, added, deleted }: DiffValuesResult
object.
Returns added, changed and deleted paths between original
and current
.
original: Input
- the original object.current: Input
- the current (actual) object.- returns
{ changed, added, deleted }: DiffPathsResult
object.
function revert(dest: Input, src: Input, customizer: (d: unknown, s: unknown) => unknown): RecordUnknown | ArrayUnknown;
Reverts dest
object to src
, calls customizer
for each dest.path
.
dest: Input
- the destination object.src: Input
- the source object.customizer: (d: unknown, s: unknown) => unknown)
- the function that is called for eachdest.path
.- returns a record or an array.
Returns all paths of the object.
obj: Input
- the source object.- returns the list of paths.
Returns the object without excludedPaths
.
obj: Input
- the source object.excludedPaths
- the array of paths to exclude. The path can be with mask:*.name
orname.*
to exclude only path started or ended with the name.- returns a record or an array.
Licensed under the MIT license.
Alexander Mac