https://github.com/ali-master/json-diff-patch
JsonDiffPatch is a library that allows for the diffing and patching of JSON objects
https://github.com/ali-master/json-diff-patch
diff diff-algorithm json json-diff-patch
Last synced: 10 months ago
JSON representation
JsonDiffPatch is a library that allows for the diffing and patching of JSON objects
- Host: GitHub
- URL: https://github.com/ali-master/json-diff-patch
- Owner: ali-master
- License: mit
- Created: 2024-07-20T09:12:30.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-07-20T10:03:21.000Z (over 1 year ago)
- Last Synced: 2025-02-25T08:40:12.043Z (10 months ago)
- Topics: diff, diff-algorithm, json, json-diff-patch
- Language: TypeScript
- Homepage: https://ali-master.github.io/json-diff-patch/
- Size: 177 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# JsonDiffPatch
JsonDiffPatch is a library that allows for the diffing and patching of JSON objects.
```bash
npm install json-diff-patch-v2
```
1. Import JsonDiffPatch in your project:
```javascript
import { DiffPatcher } from 'json-diff-patch-v2';
```
2. Create a `DiffPatcher` instance:
```javascript
const diffPatcher = new DiffPatcher();
```
3. Use the `diff`, `patch`, and `reverse` methods to work with your JSON objects:
- **Diff**: To find the difference between two objects.
- **Patch**: To apply a patch to an object.
- **Reverse**: To reverse a patch.
## Examples
### Diffing Two Objects
```javascript
const left = { name: 'John', age: 25 };
const right = { name: 'John', age: 26 };
const delta = diffPatcher.diff(left, right);
console.log(delta);
// Output: { age: [25, 26] }
```
### Patching an Object
```javascript
const original = { name: 'John', age: 25 };
const delta = { age: [25, 26] };
const patched = diffPatcher.patch(original, delta);
console.log(patched);
// Output: { name: 'John', age: 26 }
```
### Using Property Filter
In scenarios where you want to ignore certain properties during diffing, you can use the `propertyFilter` option.
```javascript
const options = {
propertyFilter: function(name) {
return name.slice(0, 1) !== '$';
},
};
const diffPatcherWithFilter = new DiffPatcher(options);
const left = { data: { $volatile: 123, stable: 456 } };
const right = { data: { $volatile: 124, stable: 456 } };
const delta = diffPatcherWithFilter.diff(left, right);
console.log(delta);
// Output: undefined (since the change is in a filtered property)
```