Gathering detailed insights and metrics for rc-jsondiffpatch
Gathering detailed insights and metrics for rc-jsondiffpatch
npm install rc-jsondiffpatch
Typescript
Module System
Min. Node Version
Node Version
NPM Version
75.9
Supply Chain
99.2
Quality
74.8
Maintenance
100
Vulnerability
99.6
License
JavaScript (99.81%)
Makefile (0.19%)
Total Downloads
2,190
Last Day
1
Last Week
4
Last Month
10
Last Year
322
330 Commits
1 Watching
5 Branches
Minified
Minified + Gzipped
Latest Version
0.0.1
Package Id
rc-jsondiffpatch@0.0.1
Unpacked Size
3.09 MB
Size
553.78 kB
File Count
15
NPM Version
7.20.1
Node Version
16.13.2
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
300%
4
Compared to previous week
Last month
233.3%
10
Compared to previous month
Last year
-66.2%
322
Compared to previous year
2
33
Diff & patch JavaScript objects
/dist
folder with bundles for UMD, commonjs, or ES modules)objectHash
function (this is how objects are matched, otherwise a dumb match by position is used). For more details, check Array diff documentation./node_modules/.bin/jsondiffpatch left.json right.json
jsondiffpatch.clone(obj)
(deep clone)And you can test your current browser visiting the test page.
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
This works for node, or in browsers if you already do bundling on your app
1npm install jsondiffpatch
1var jsondiffpatch = require('jsondiffpatch').create(options);
In a browser, you could load directly a bundle in /dist
, eg. /dist/jsondiffpatch.umd.js
.
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});
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
1# diff two json files, colored output (using chalk lib) 2./node_modules/.bin/jsondiffpatch ./left.json ./right.json 3 4# or install globally 5npm install -g jsondiffpatch 6 7jsondiffpatch ./demo/left.json ./demo/right.json
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.
No security vulnerabilities found.