Javascript utility for calculating deep difference, capturing changes, and applying changes across objects; for nodejs and the browser.
Installations
npm install deep-diff
Score
99.7
Supply Chain
99.6
Quality
75.8
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Developer
flitbit
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
No
Node Version
8.11.3
NPM Version
5.6.0
Statistics
3,004 Stars
222 Commits
213 Forks
36 Watching
2 Branches
32 Contributors
Updated on 28 Nov 2024
Bundle Size
5.48 kB
Minified
1.90 kB
Minified + Gzipped
Languages
JavaScript (97.64%)
HTML (2.36%)
Total Downloads
Cumulative downloads
Total Downloads
422,847,602
Last day
-29.4%
250,866
Compared to previous day
Last week
-8.3%
1,688,666
Compared to previous week
Last month
5.2%
7,395,467
Compared to previous month
Last year
14.9%
78,906,228
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
deep-diff
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.
Install
1npm install deep-diff
Possible v1.0.0 incompatabilities:
- elements in arrays are now processed in reverse order, which fixes a few nagging bugs but may break some users
- If your code relied on the order in which the differences were reported then your code will break. If you consider an object graph to be a big tree, then
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.
- If your code relied on the order in which the differences were reported then your code will break. If you consider an object graph to be a big tree, then
Features
- Get the structural differences between two objects.
- Observe the structural differences between two objects.
- When structural differences represent change, apply change from one object to another.
- When structural differences represent change, selectively apply change from one object to another.
Installation
1npm install deep-diff
Importing
nodejs
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';
browser
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();
.
Simple Examples
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
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 array
path
- 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 index
Change 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.
Changes
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});
API Documentation
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.
Arguments
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 theoptions
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 apush
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}
Pre-filtering Object Properties
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');
Normalizing object properties
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.
Argmuments
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 theoptions
object in the parameters for backward compatibility.normalize
: function that pre-processes every leaf of the tree.
Contributing
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
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
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
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 16 are checked with a SAST tool
Reason
41 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-6chw-6frg-f759
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-c6rq-rjc2-86v2
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-hr2v-3952-633q
- Warn: Project is vulnerable to: GHSA-ff7x-qrg7-qggm
- Warn: Project is vulnerable to: GHSA-8r6j-v8pm-fqw3
- Warn: Project is vulnerable to: MAL-2023-462
- Warn: Project is vulnerable to: GHSA-pfrx-2q88-qq97
- Warn: Project is vulnerable to: GHSA-qqgx-2p2h-9c37
- Warn: Project is vulnerable to: GHSA-2pr6-76vf-7546
- Warn: Project is vulnerable to: GHSA-8j8c-7jfh-h6hx
- Warn: Project is vulnerable to: GHSA-3c6g-pvg8-gqw2
- Warn: Project is vulnerable to: GHSA-rrqv-vjrw-hrcr
- Warn: Project is vulnerable to: GHSA-x5r6-x823-9848
- Warn: Project is vulnerable to: GHSA-8gwj-8hxc-285w
- Warn: Project is vulnerable to: GHSA-6c8f-qphg-qjgp
- Warn: Project is vulnerable to: GHSA-4xc9-xhrj-v574
- Warn: Project is vulnerable to: GHSA-x5rq-j2xg-h7qm
- Warn: Project is vulnerable to: GHSA-jf85-cpcp-j695
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m / GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-fhjf-83wg-r2j9
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-4g88-fppr-53pp
- Warn: Project is vulnerable to: GHSA-4jqc-8m5r-9rpr
- Warn: Project is vulnerable to: GHSA-j44m-qm6p-hp7m
- Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9
- Warn: Project is vulnerable to: GHSA-r628-mhmh-qjhw
- Warn: Project is vulnerable to: GHSA-9r2w-394v-53qc
- Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh
- Warn: Project is vulnerable to: GHSA-qq89-hq3f-393p
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-332q-7ff2-57h2
Score
2
/10
Last Scanned on 2024-11-25
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 MoreOther packages similar to 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