Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

json-diff-patch-v2

ali-master47MIT1.1.2TypeScript support: included

Diffing and patching of JSON objects

json, diff, patch, json-diff, json-patch, json-diff-patch

readme

JsonDiffPatch

JsonDiffPatch is a library that allows for the diffing and patching of JSON objects.

npm install json-diff-patch-v2
  1. Import JsonDiffPatch in your project:
import { DiffPatcher } from 'json-diff-patch-v2';
  1. Create a DiffPatcher instance:
const diffPatcher = new DiffPatcher();
  1. Use the diff, patch, and reverse methods to work with your JSON objects:

  2. Diff: To find the difference between two objects.

  3. Patch: To apply a patch to an object.
  4. Reverse: To reverse a patch.

Examples

Diffing Two Objects

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

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.

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)

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

1.1.2 (2024-07-20)

Bug Fixes

  • npm: add publish access and keywords (d142bcf)
  • npm: rename package name (31346da)

1.1.1 (2024-07-20)

Bug Fixes

  • npm: rename package name (cf04f1c)
  • npm: rename package name (90afd9a)

1.1.0 (2024-07-20)

Features

Bug Fixes