Gathering detailed insights and metrics for @ziflow/json-diff-ts
Gathering detailed insights and metrics for @ziflow/json-diff-ts
npm install @ziflow/json-diff-ts
Typescript
Module System
Node Version
NPM Version
TypeScript (98.52%)
JavaScript (1.48%)
Total Downloads
275
Last Day
2
Last Week
3
Last Month
7
Last Year
49
111 Stars
299 Commits
27 Forks
4 Watching
10 Branches
12 Contributors
Minified
Minified + Gzipped
Latest Version
1.2.6
Package Id
@ziflow/json-diff-ts@1.2.6
Unpacked Size
33.26 kB
Size
8.48 kB
File Count
9
NPM Version
8.19.2
Node Version
16.18.1
Publised On
17 Feb 2023
Cumulative downloads
Total Downloads
Last day
100%
2
Compared to previous day
Last week
200%
3
Compared to previous week
Last month
600%
7
Compared to previous month
Last year
-78.3%
49
Compared to previous year
1
json-diff-ts
is a TypeScript library that calculates and applies differences between JSON objects. A standout feature is its ability to identify elements in arrays using keys instead of indices, which offers a more intuitive way to handle arrays. It also supports JSONPath, a query language for JSON, which enables you to target specific parts of a JSON document with precision.
Another significant feature of this library is its ability to transform changesets into atomic changes. This means that each change in the data can be isolated and applied independently, providing a granular level of control over the data manipulation process.
This library is particularly valuable for applications where tracking changes in JSON data is crucial. It simplifies the process of comparing JSON objects and applying changes. The support for key-based array identification can be especially useful in complex JSON structures where tracking by index is not efficient or intuitive. JSONPath support further enhances its capabilities by allowing precise targeting of specific parts in a JSON document, making it a versatile tool for handling JSON data.
Starting with version 3, json-diff-ts
now supports both ECMAScript Modules and CommonJS. This makes the library more versatile and compatible with various JavaScript environments. Whether you're working in a modern project that uses ES modules, or a Node.js project that uses CommonJS, you can now use json-diff-ts
seamlessly.
1npm install json-diff-ts
In TypeScript or ES Modules, you can import the diff
function like this:
1import { diff } from 'json-diff-ts';
In CommonJS, you can import the diff function like this:
1const { diff } = require('json-diff-ts');
diff
Generates a difference set for JSON objects. When comparing arrays, if a specific key is provided, differences are determined by matching elements via this key rather than array indices.
1import { diff } from 'json-diff-ts'; 2 3const oldData = { 4 planet: 'Tatooine', 5 faction: 'Jedi', 6 characters: [ 7 { id: 'LUK', name: 'Luke Skywalker', force: true }, 8 { id: 'LEI', name: 'Leia Organa', force: true } 9 ], 10 weapons: ['Lightsaber', 'Blaster'] 11}; 12 13const newData = { 14 planet: 'Alderaan', 15 faction: 'Rebel Alliance', 16 characters: [ 17 { id: 'LUK', name: 'Luke Skywalker', force: true, rank: 'Commander' }, 18 { id: 'HAN', name: 'Han Solo', force: false } 19 ], 20 weapons: ['Lightsaber', 'Blaster', 'Bowcaster'] 21}; 22 23const diffs = diff(oldData, newData, { embeddedObjKeys: { characters: 'id' } }); 24 25const expectedDiffs = [ 26 { 27 type: 'UPDATE', 28 key: 'planet', 29 value: 'Alderaan', 30 oldValue: 'Tatooine' 31 }, 32 { 33 type: 'UPDATE', 34 key: 'faction', 35 value: 'Rebel Alliance', 36 oldValue: 'Jedi' 37 }, 38 { 39 type: 'UPDATE', 40 key: 'characters', 41 embeddedKey: 'id', 42 changes: [ 43 { 44 type: 'UPDATE', 45 key: 'LUK', 46 changes: [ 47 { 48 type: 'ADD', 49 key: 'rank', 50 value: 'Commander' 51 } 52 ] 53 }, 54 { 55 type: 'ADD', 56 key: 'HAN', 57 value: { 58 id: 'HAN', 59 name: 'Han Solo', 60 force: false 61 } 62 }, 63 { 64 type: 'REMOVE', 65 key: 'LEI', 66 value: { 67 id: 'LEI', 68 name: 'Leia Organa', 69 force: true 70 } 71 } 72 ] 73 }, 74 { 75 type: 'UPDATE', 76 key: 'weapons', 77 embeddedKey: '$index', 78 changes: [ 79 { 80 type: 'ADD', 81 key: '2', 82 value: 'Bowcaster' 83 } 84 ] 85 } 86];
Paths can be utilized to identify keys within nested arrays.
1const diffs = diff(oldData, newData, { embeddedObjKeys { 'characters.subarray': 'id' }});
You can also designate the root by using '.' instead of an empty string ('').
1const diffs = diff(oldData, newData, { embeddedObjKeys: { '.characters.subarray': 'id' } });
Determine if type changes are treated as a replace (remove, add) or as an update; default is replace.
1const diffs = diff(oldData, newData, { treatTypeChangeAsReplace: false });
You can use a function to dynamically resolve the key of the object. The first parameter is the object and the second is to signal if the function should return the key name instead of the value. This is needed to flatten the changeset
1const diffs = diff(oldData, newData, { 2 embeddedObjKeys: { 3 characters: (obj, shouldReturnKeyName) => (shouldReturnKeyName ? 'id' : obj.id) 4 } 5});
If you're using the Map type, you can employ regular expressions for path identification.
1const embeddedObjKeys: EmbeddedObjKeysMapType = new Map(); 2 3embeddedObjKeys.set(/^char\w+$/, 'id'); // instead of 'id' you can specify a function 4 5const diffs = diff(oldObj, newObj, { embeddedObjKeys });
Compare string arrays by value instead of index
1const diffs = diff(oldObj, newObj, { embeddedObjKeys: { stringArr: '$value' } });
atomizeChangeset
Transforms a complex changeset into a list of atomic changes, each describable by a JSONPath.
1const atomicChanges = atomizeChangeset(diffs); 2// Restore the changeset from a selection of atomic changes 3const changeset = unatomizeChangeset(flatChanges.slice(0, 3)); 4// Alternatively, apply the changes using a JSONPath-capable library 5// ...
Atomic Changes will have the following structure:
1[ 2 { type: 'UPDATE', key: 'planet', value: 'Alderaan', oldValue: 'Tatooine', path: '$.planet', valueType: 'String' }, 3 // ... Additional flat changes here 4 { type: 'ADD', key: 'rank', value: 'Commander', path: "$.characters[?(@.id=='LUK')].rank", valueType: 'String' } 5];
applyChange
1const oldData = { 2 // ... Initial data here 3}; 4 5// Sample diffs array, similar to the one generated in the diff example 6const diffs = [ 7 // ... Diff objects here 8]; 9 10changesets.applyChanges(oldData, diffs); 11 12expect(oldData).to.eql({ 13 // ... Updated data here 14});
revertChange
1const newData = { 2 // ... Updated data here 3}; 4 5// Sample diffs array 6const diffs = [ 7 // ... Diff objects here 8]; 9 10changesets.revertChanges(newData, diffs); 11 12expect(newData).to.eql({ 13 // ... Original data restored here 14});
jsonPath
The json-diff-ts
library uses JSONPath to address specific parts of a JSON document in both the changeset and the application/reversion of changes.
1 2const jsonPath = changesets.jsonPath; 3 4cost data = { 5 // ... Some JSON data 6}; 7 8const value = jsonPath.query(data, '$.characters[?(@.id=="LUK")].name'); 9 10expect(value).to.eql(['Luke Skywalker']);
Contributions are welcome! Please follow the provided issue templates and code of conduct.
Reach out to the maintainer via LinkedIn or Twitter:
Discover more about the company behind this project: hololux
This project takes inspiration and code from diff-json by viruschidai@gmail.com.
json-diff-ts is open-sourced software licensed under the MIT license.
The original diff-json project is also under the MIT License. For more information, refer to its license details.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
SAST tool detected but not run on all commits
Details
Reason
3 existing vulnerabilities detected
Details
Reason
Found 2/5 approved changesets -- score normalized to 4
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
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
project is not fuzzed
Details
Reason
security policy file not detected
Details
Score
Last Scanned on 2025-01-27
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