Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@broofa/jsondiff

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@broofa/jsondiff

Pragmatic, intuitive diffing and patching of JSON objects

  • 1.3.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
114
decreased by-29.19%
Maintainers
1
Weekly downloads
 
Created
Source

@broofa/jsondiff

Pragmatic, intuitive diffing and patching of JSON objects

There are variety of modules available that can diff and patch JSON data structures. Here's a quick run down:

ModuleSizePatch formatNotes
@broofa/jsondiff0.7KOverlayReadable patches
deep-diff3.5KRFC9602-likeMost popular module
rfc96022KRFC9602See below
fast-json-patch4KRFC9602See below

The main difference between these modules is in the patch object structure. Most (all?) of the other modules use a structure based on or similar to RFC9602, which is composed of a series of operations that describe how to transform the target object. In contrast, the patches in this module act as an "overlay" that is copied onto the target object. There are tradeoffs to this, some good, some bad:

  1. Readability - A structured patch is easier to read because it "looks" like the object it's modifying.
  2. Reordering - Data is not "moved" in a structured patch. It is simply deleted from the old location and inserted at the new location.
  3. Size - Operation-based patches are more verbose (lots of duplicate keys and values), except in the case where values move locations. This may have a significant impact on network bandwidth, especially for uncompressed data streams.
  4. Fault tolerance - Operation-based patches may fail if operations are applied out of order or if the target object does not have the expected structure.
  5. DROP/KEEP hack - See comments about DROP and KEEP values in "Patch Objects", below. This may be off-putting to some readers.

Installation

npm install @broofa/jsondiff

Usage

const jsondiff = require('@broofa/jsondiff');

const before = {a: 'Hello', b: 'you', c: ['big', 'bad'], d: 'beast'};
const after = {a: 'Hi', c: ['big', 'bad', 'bold'], d: 'beast'};

// Create a patch
// Note the use of DROP (-) and KEEP(+) values
const patch = jsondiff.diff(before, after); // ⇨ { a: 'Hi', b: '-', c: [ '+', '+', 'bold' ] }

// Apply it to the original
const patched = jsondiff.patch(before, patch); // ⇨ { a: 'Hi', c: [ 'big', 'bad', 'bold' ], d: 'beast' }

// Get the expected result
assert.deepEqual(after, patched); // Passes!

API

jsondiff.diff(before, after)

Creates and returns a "patch object" that describes the differences between before and after. This object is suitable for use in patch().

jsondiff.patch(before, patch)

Applies a patch object to before and returns the result.

Note: Any result value that is deep-equal to it's before counterpart will reference the 'before' value directly, allowing === to be used as a test for deep equality.

jsondiff.merge(before, after)

Shorthand for jsondiff.patch(before, jsondiff.diff(before, after)). Useful for mutating an object only where values have actually changed.

Patch Objects

Patch objects are JSON objects with the same structure (schema) as the object they apply to. Applying a patch is (almost) as simple as doing a deep copy of the patch onto the target object. There are two special cases:

  1. jsondiff.DROP ("-") values are "dropped" (deleted or set to undefined)
  2. jsondiff.KEEP ("+") values are "kept" (resolve to the corresponding value in the target)

Note: DROP and KEEP are, admittedly, a hack. If these exact string values appear in data outside of patch objects, diff() and patch() may not function correctly. That said, this is not expected to be an issue in real-world conditions. (Both strings include a "private use" Unicode character that should make them fairly unique.)


Markdown generated from src/README_js.md by RunMD Logo

Keywords

FAQs

Package last updated on 01 Mar 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc