Installations
npm install jsondiffpatch-rc
Developer Guide
Typescript
Yes
Module System
CommonJS, UMD
Min. Node Version
>=8.17.0
Node Version
16.14.0
NPM Version
8.3.1
Releases
Contributors
Languages
TypeScript (95.28%)
CSS (3.53%)
JavaScript (1.19%)
Developer
benjamine
Download Statistics
Total Downloads
89,692
Last Day
500
Last Week
1,671
Last Month
7,511
Last Year
57,431
GitHub Statistics
4,910 Stars
372 Commits
474 Forks
89 Watching
2 Branches
38 Contributors
Bundle Size
53.75 kB
Minified
15.89 kB
Minified + Gzipped
Package Meta Information
Latest Version
0.4.2
Package Id
jsondiffpatch-rc@0.4.2
Unpacked Size
3.08 MB
Size
551.74 kB
File Count
15
NPM Version
8.3.1
Node Version
16.14.0
Total Downloads
Cumulative downloads
Total Downloads
89,692
Last day
3.1%
500
Compared to previous day
Last week
-2.7%
1,671
Compared to previous week
Last month
6.6%
7,511
Compared to previous month
Last year
81%
57,431
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Dev Dependencies
33
jsondiffpatch-rc
Diff & patch JavaScript objects
Live Demo
- min+gzipped ~ 16KB
- browser and server (
/dist
folder with bundles for UMD, commonjs, or ES modules) - (optionally) uses google-diff-match-patch for long text diffs (diff at character level)
- smart array diffing using LCS, IMPORTANT NOTE: to match objects inside an array you must provide an
objectHash
function (this is how objects are matched, otherwise a dumb match by position is used). For more details, check Array diff documentation - reverse a delta
- unpatch (eg. revert object to its original state using a delta)
- simplistic, pure JSON, low footprint delta format
- multiple output formatters:
- html (check it at the Live Demo)
- annotated json (html), makes the JSON delta format self-explained
- console (colored), try running
./node_modules/.bin/jsondiffpatch left.json right.json
- JSON Patch format RFC 6902 support
- write your own! check Formatters documentation
- BONUS:
jsondiffpatch.clone(obj)
(deep clone)
Supported platforms
- Any modern browser and IE8+
And you can test your current browser visiting the test page.
Usage
1 // sample data 2 var country = { 3 name: "Argentina", 4 capital: "Buenos Aires", 5 independence: new Date(1816, 6, 9), 6 unasur: true 7 }; 8 9 // clone country, using dateReviver for Date objects 10 var country2 = JSON.parse(JSON.stringify(country), jsondiffpatch.dateReviver); 11 12 // make some changes 13 country2.name = "Republica Argentina"; 14 country2.population = 41324992; 15 delete country2.capital; 16 17 var delta = jsondiffpatch.diff(country, country2); 18 19 assertSame(delta, { 20 "name":["Argentina","Republica Argentina"], // old value, new value 21 "population":["41324992"], // new value 22 "capital":["Buenos Aires", 0, 0] // deleted 23 }); 24 25 // patch original 26 jsondiffpatch.patch(country, delta); 27 28 // reverse diff 29 var reverseDelta = jsondiffpatch.reverse(delta); 30 // also country2 can be return to original value with: jsondiffpatch.unpatch(country2, delta); 31 32 var delta2 = jsondiffpatch.diff(country, country2); 33 assert(delta2 === undefined) 34 // undefined => no difference
Array diffing:
1 // sample data 2 var country = { 3 name: "Argentina", 4 cities: [ 5 { 6 name: 'Buenos Aires', 7 population: 13028000, 8 }, 9 { 10 name: 'Cordoba', 11 population: 1430023, 12 }, 13 { 14 name: 'Rosario', 15 population: 1136286, 16 }, 17 { 18 name: 'Mendoza', 19 population: 901126, 20 }, 21 { 22 name: 'San Miguel de Tucuman', 23 population: 800000, 24 } 25 ] 26 }; 27 28 // clone country 29 var country2 = JSON.parse(JSON.stringify(country)); 30 31 // delete Cordoba 32 country.cities.splice(1, 1); 33 34 // add La Plata 35 country.cities.splice(4, 0, { 36 name: 'La Plata' 37 }); 38 39 // modify Rosario, and move it 40 var rosario = country.cities.splice(1, 1)[0]; 41 rosario.population += 1234; 42 country.cities.push(rosario); 43 44 // create a configured instance, match objects by name 45 var diffpatcher = jsondiffpatch.create({ 46 objectHash: function(obj) { 47 return obj.name; 48 } 49 }); 50 51 var delta = diffpatcher.diff(country, country2); 52 53 assertSame(delta, { 54 "cities": { 55 "_t": "a", // indicates this node is an array (not an object) 56 "1": [ 57 // inserted at index 1 58 { 59 "name": "Cordoba", 60 "population": 1430023 61 }] 62 , 63 "2": { 64 // population modified at index 2 (Rosario) 65 "population": [ 66 1137520, 67 1136286 68 ] 69 }, 70 "_3": [ 71 // removed from index 3 72 { 73 "name": "La Plata" 74 }, 0, 0], 75 "_4": [ 76 // move from index 4 to index 2 77 '', 2, 3] 78 } 79 });
For more example cases (nested objects or arrays, long text diffs) check test/examples/
If you want to understand deltas, see delta format documentation
Installing
NPM
This works for node, or in browsers if you already do bundling on your app
1npm install jsondiffpatch
1var jsondiffpatch = require('jsondiffpatch').create(options);
browser
In a browser, you could load directly a bundle in /dist
, eg. /dist/jsondiffpatch.umd.js
.
Options
1var jsondiffpatch = require('jsondiffpatch').create({ 2 // used to match objects when diffing arrays, by default only === operator is used 3 objectHash: function(obj) { 4 // this function is used only to when objects are not equal by ref 5 return obj._id || obj.id; 6 }, 7 arrays: { 8 // default true, detect items moved inside the array (otherwise they will be registered as remove+add) 9 detectMove: true, 10 // default false, the value of items moved is not included in deltas 11 includeValueOnMove: false 12 }, 13 textDiff: { 14 // default 60, minimum string length (left and right sides) to use text diff algorythm: google-diff-match-patch 15 minLength: 60 16 }, 17 propertyFilter: function(name, context) { 18 /* 19 this optional function can be specified to ignore object properties (eg. volatile data) 20 name: property name, present in either context.left or context.right objects 21 context: the diff context (has context.left and context.right objects) 22 */ 23 return name.slice(0, 1) !== '$'; 24 }, 25 cloneDiffValues: false /* default false. if true, values in the obtained delta will be cloned 26 (using jsondiffpatch.clone by default), to ensure delta keeps no references to left or right objects. this becomes useful if you're diffing and patching the same objects multiple times without serializing deltas. 27 instead of true, a function can be specified here to provide a custom clone(value) 28 */ 29});
Visual Diff
1<!DOCTYPE html> 2<html> 3 <head> 4 <script type='text/javascript' src="https://cdn.jsdelivr.net/npm/jsondiffpatch/dist/jsondiffpatch.umd.min.js"></script> 5 <link rel="stylesheet" href="./style.css" type="text/css" /> 6 <link rel="stylesheet" href="../formatters-styles/html.css" type="text/css" /> 7 <link rel="stylesheet" href="../formatters-styles/annotated.css" type="text/css" /> 8 </head> 9 <body> 10 <div id="visual"></div> 11 <hr/> 12 <div id="annotated"></div> 13 <script> 14 var left = { a: 3, b: 4 }; 15 var right = { a: 5, c: 9 }; 16 var delta = jsondiffpatch.diff(left, right); 17 18 // beautiful html diff 19 document.getElementById('visual').innerHTML = jsondiffpatch.formatters.html.format(delta, left); 20 21 // self-explained json 22 document.getElementById('annotated').innerHTML = jsondiffpatch.formatters.annotated.format(delta, left); 23 </script> 24 </body> 25</html>
To see formatters in action check the Live Demo.
For more details check Formatters documentation
Plugins
diff()
, patch()
and reverse()
functions are implemented using Pipes & Filters pattern, making it extremely customizable by adding or replacing filters on a pipe.
Check Plugins documentation for details.
No vulnerabilities found.
Reason
no vulnerabilities detected
Reason
no dangerous workflow patterns detected
Reason
tokens are read-only in GitHub workflows
Reason
license file detected
Details
- Info: : MIT-LICENSE.txt:1
Reason
all dependencies are pinned
Details
- Info: GitHub-owned GitHubActions are pinned
- Info: Third-party GitHubActions are pinned
- Info: Dockerfile dependencies are pinned
- Info: no insecure (not pinned by hash) dependency downloads found in Dockerfiles
- Info: no insecure (not pinned by hash) dependency downloads found in shell scripts
Reason
no binaries found in the repo
Reason
GitHub code reviews found for 23 commits out of the last 30 -- score normalized to 7
Details
- Warn: no reviews found for commit: ee7a3c96a52abaa0bbac4b7db5b479496786c533
- Warn: no reviews found for commit: f06f64df90b54128179e31438a5c981da991450a
- Warn: no reviews found for commit: 2e1b8203a6d2b516c73783960e5b366ef61c506a
- Warn: no reviews found for commit: 0d547721d7e0d5c6bdcd319711157b4f5303bb12
- Warn: no reviews found for commit: 6afbbdc82891deeb8a80ec239b100a84f25a468b
- Warn: no reviews found for commit: 83afa4d4c7baf76dfeff53b69b59666af1e6046d
- Warn: no reviews found for commit: 0c4323e9bff23ae231f4bff231ceed4df2b48be5
Reason
0 commit(s) out of 30 and 0 issue activity out of 30 found in the last 90 days -- score normalized to 0
Reason
no badge detected
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
security policy file not detected
Reason
no update tool detected
Details
- Warn: dependabot config file not detected in source location. We recommend setting this configuration in code so it can be easily verified by others.
- Warn: renovatebot config file not detected in source location. We recommend setting this configuration in code so it can be easily verified by others.
Reason
project is not fuzzed
Score
5.5
/10
Last Scanned on 2022-08-15
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