Gathering detailed insights and metrics for deep-diff
Gathering detailed insights and metrics for deep-diff
Gathering detailed insights and metrics for deep-diff
Gathering detailed insights and metrics for deep-diff
@types/deep-diff
TypeScript definitions for deep-diff
@bundled-es-modules/deep-diff
mirror of deep-diff, bundled and exposed as ES module
deep-diff-pizza
Deep Diff Pizza is a simple, 0 dependency utility function that takes in 2 JSON Objects and returns the differences in an easy-to-use format.
redux-deep-diff
Higher order reducer to deep diff redux states
Javascript utility for calculating deep difference, capturing changes, and applying changes across objects; for nodejs and the browser.
npm install deep-diff
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
3,003 Stars
222 Commits
213 Forks
36 Watching
2 Branches
32 Contributors
Updated on 22 Nov 2024
JavaScript (97.64%)
HTML (2.36%)
Cumulative downloads
Total Downloads
Last day
-3.3%
350,132
Compared to previous day
Last week
2.8%
1,831,942
Compared to previous week
Last month
15.3%
7,477,956
Compared to previous month
Last year
14.9%
78,894,515
Compared to previous year
deep-diff is a javascript/node.js module providing utility functions for determining the structural differences between objects and includes some utilities for applying differences across objects.
1npm install deep-diff
Possible v1.0.0 incompatabilities:
deep-diff
does a pre-order traversal of the object graph, however, when it encounters an array, the array is processed from the end towards the front, with each element recursively processed in-order during further descent.1npm install deep-diff
1var diff = require('deep-diff') 2// or: 3// const diff = require('deep-diff'); 4// const { diff } = require('deep-diff'); 5// or: 6// const DeepDiff = require('deep-diff'); 7// const { DeepDiff } = require('deep-diff'); 8// es6+: 9// import diff from 'deep-diff'; 10// import { diff } from 'deep-diff'; 11// es6+: 12// import DeepDiff from 'deep-diff'; 13// import { DeepDiff } from 'deep-diff';
1<script src="https://cdn.jsdelivr.net/npm/deep-diff@1/dist/deep-diff.min.js"></script>
In a browser,
deep-diff
defines a global variableDeepDiff
. If there is a conflict in the global namespace you can restore the conflicting definition and assigndeep-diff
to another variable like this:var deep = DeepDiff.noConflict();
.
In order to describe differences, change revolves around an origin
object. For consistency, the origin
object is always the operand on the left-hand-side
of operations. The comparand
, which may contain changes, is always on the right-hand-side
of operations.
1var diff = require('deep-diff').diff; 2 3var lhs = { 4 name: 'my object', 5 description: 'it\'s an object!', 6 details: { 7 it: 'has', 8 an: 'array', 9 with: ['a', 'few', 'elements'] 10 } 11}; 12 13var rhs = { 14 name: 'updated object', 15 description: 'it\'s an object!', 16 details: { 17 it: 'has', 18 an: 'array', 19 with: ['a', 'few', 'more', 'elements', { than: 'before' }] 20 } 21}; 22 23var differences = diff(lhs, rhs);
v 0.2.0 and above The code snippet above would result in the following structure describing the differences:
1[ { kind: 'E', 2 path: [ 'name' ], 3 lhs: 'my object', 4 rhs: 'updated object' }, 5 { kind: 'E', 6 path: [ 'details', 'with', 2 ], 7 lhs: 'elements', 8 rhs: 'more' }, 9 { kind: 'A', 10 path: [ 'details', 'with' ], 11 index: 3, 12 item: { kind: 'N', rhs: 'elements' } }, 13 { kind: 'A', 14 path: [ 'details', 'with' ], 15 index: 4, 16 item: { kind: 'N', rhs: { than: 'before' } } } ]
Differences are reported as one or more change records. Change records have the following structure:
kind
- indicates the kind of change; will be one of the following:
N
- indicates a newly added property/elementD
- indicates a property/element was deletedE
- indicates a property/element was editedA
- indicates a change occurred within an arraypath
- the property path (from the left-hand-side root)lhs
- the value on the left-hand-side of the comparison (undefined if kind === 'N')rhs
- the value on the right-hand-side of the comparison (undefined if kind === 'D')index
- when kind === 'A', indicates the array index where the change occurreditem
- when kind === 'A', contains a nested change record indicating the change that occurred at the array indexChange records are generated for all structural differences between origin
and comparand
. The methods only consider an object's own properties and array elements; those inherited from an object's prototype chain are not considered.
Changes to arrays are recorded simplistically. We care most about the shape of the structure; therefore we don't take the time to determine if an object moved from one slot in the array to another. Instead, we only record the structural
differences. If the structural differences are applied from the comparand
to the origin
then the two objects will compare as "deep equal" using most isEqual
implementations such as found in lodash or underscore.
When two objects differ, you can observe the differences as they are calculated and selectively apply those changes to the origin object (left-hand-side).
1var observableDiff = require('deep-diff').observableDiff; 2var applyChange = require('deep-diff').applyChange; 3 4var lhs = { 5 name: 'my object', 6 description: 'it\'s an object!', 7 details: { 8 it: 'has', 9 an: 'array', 10 with: ['a', 'few', 'elements'] 11 } 12}; 13 14var rhs = { 15 name: 'updated object', 16 description: 'it\'s an object!', 17 details: { 18 it: 'has', 19 an: 'array', 20 with: ['a', 'few', 'more', 'elements', { than: 'before' }] 21 } 22}; 23 24observableDiff(lhs, rhs, function (d) { 25 // Apply all changes except to the name property... 26 if (d.path[d.path.length - 1] !== 'name') { 27 applyChange(lhs, rhs, d); 28 } 29});
A standard import of var diff = require('deep-diff')
is assumed in all of the code examples. The import results in an object having the following public properties:
diff(lhs, rhs[, options, acc])
— calculates the differences between two objects, optionally using the specified accumulator.observableDiff(lhs, rhs, observer[, options])
— calculates the differences between two objects and reports each to an observer function.applyDiff(target, source, filter)
— applies any structural differences from a source object to a target object, optionally filtering each difference.applyChange(target, source, change)
— applies a single change record to a target object. NOTE: source
is unused and may be removed.revertChange(target, source, change)
reverts a single change record to a target object. NOTE: source
is unused and may be removed.diff
The diff
function calculates the difference between two objects.
lhs
- the left-hand operand; the origin object.rhs
- the right-hand operand; the object being compared structurally with the origin object.options
- A configuration object that can have the following properties:
prefilter
: function that determines whether difference analysis should continue down the object graph. This function can also replace the options
object in the parameters for backward compatibility.normalize
: function that pre-processes every leaf of the tree.acc
- an optional accumulator/array (requirement is that it have a push
function). Each difference is pushed to the specified accumulator.Returns either an array of changes or, if there are no changes, undefined
. This was originally chosen so the result would be pass a truthy test:
1var changes = diff(obja, objb); 2if (changes) { 3 // do something with the changes. 4}
The prefilter
's signature should be function(path, key)
and it should return a truthy value for any path
-key
combination that should be filtered. If filtered, the difference analysis does no further analysis of on the identified object-property path.
1const diff = require('deep-diff'); 2const assert = require('assert'); 3 4const data = { 5 issue: 126, 6 submittedBy: 'abuzarhamza', 7 title: 'readme.md need some additional example prefilter', 8 posts: [ 9 { 10 date: '2018-04-16', 11 text: `additional example for prefilter for deep-diff would be great. 12 https://stackoverflow.com/questions/38364639/pre-filter-condition-deep-diff-node-js` 13 } 14 ] 15}; 16 17const clone = JSON.parse(JSON.stringify(data)); 18clone.title = 'README.MD needs additional example illustrating how to prefilter'; 19clone.disposition = 'completed'; 20 21const two = diff(data, clone); 22const none = diff(data, clone, 23 (path, key) => path.length === 0 && ~['title', 'disposition'].indexOf(key) 24); 25 26assert.equal(two.length, 2, 'should reflect two differences'); 27assert.ok(typeof none === 'undefined', 'should reflect no differences');
The normalize
's signature should be function(path, key, lhs, rhs)
and it should return either a falsy value if no normalization has occured, or a [lhs, rhs]
array to replace the original values. This step doesn't occur if the path was filtered out in the prefilter
phase.
1const diff = require('deep-diff'); 2const assert = require('assert'); 3 4const data = { 5 pull: 149, 6 submittedBy: 'saveman71', 7}; 8 9const clone = JSON.parse(JSON.stringify(data)); 10clone.issue = 42; 11 12const two = diff(data, clone); 13const none = diff(data, clone, { 14 normalize: (path, key, lhs, rhs) => { 15 if (lhs === 149) { 16 lhs = 42; 17 } 18 if (rhs === 149) { 19 rhs = 42; 20 } 21 return [lsh, rhs]; 22 } 23}); 24 25assert.equal(two.length, 1, 'should reflect one difference'); 26assert.ok(typeof none === 'undefined', 'should reflect no difference');
observableDiff
The observableDiff
function calculates the difference between two objects and reports each to an observer function.
lhs
- the left-hand operand; the origin object.rhs
- the right-hand operand; the object being compared structurally with the origin object.observer
- The observer to report to.options
- A configuration object that can have the following properties:
prefilter
: function that determines whether difference analysis should continue down the object graph. This function can also replace the options
object in the parameters for backward compatibility.normalize
: function that pre-processes every leaf of the tree.When contributing, keep in mind that it is an objective of deep-diff
to have no package dependencies. This may change in the future, but for now, no-dependencies.
Please run the unit tests before submitting your PR: npm test
. Hopefully your PR includes additional unit tests to illustrate your change/modification!
When you run npm test
, linting will be performed and any linting errors will fail the tests... this includes code formatting.
Thanks to all those who have contributed so far!
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 5/19 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
41 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More