npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@broofa/jsondiff

v1.3.6

Published

Pragmatic, intuitive diffing and patching of JSON objects

Downloads

248

Readme

@broofa/jsondiff

Pragmatic and intuitive diff and patch functions for JSON data

Installation

npm install @broofa/jsondiff

Usage

Require it:

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

// ... or ES6 module style:
// import jsondiff from '@broofa/jsondiff';

Start with some before and after state:

console.log(before);

⇒ {
⇒   name: 'my object',
⇒   description: "it's an object!",
⇒   details: { it: 'has', an: 'array', with: [ 'a', 'few', 'elements' ] }
⇒ }
console.log(after);

⇒ {
⇒   name: 'updated object',
⇒   title: "it's an object!",
⇒   details: {
⇒     it: 'has',
⇒     an: 'array',
⇒     with: [ 'a', 'few', 'more', 'elements', { than: 'before' } ]
⇒   }
⇒ }

Create a patch that descibes the difference between the two:

const patch = jsondiff.diff(before, after);
console.log(patch);

⇒ {
⇒   name: 'updated object',
⇒   description: '-',
⇒   details: { with: [ '+', '+', 'more', 'elements', { than: 'before' } ] },
⇒   title: "it's an object!"
⇒ }

(Note the special DROP and KEEP values ("-" and "+")! These are explained in Patch Objects, below.)

Apply patch to the before state to reproduce the after state:

const patched = jsondiff.patch(before, patch);
console.log(patched);

⇒ {
⇒   name: 'updated object',
⇒   details: {
⇒     it: 'has',
⇒     an: 'array',
⇒     with: [ 'a', 'few', 'more', 'elements', { than: 'before' } ]
⇒   },
⇒   title: "it's an object!"
⇒ }

Why yet-another diff module?

There are already several modules in this space - deep-diff, rfc6902, or fast-json-patch, to name a few. deep-diff is the most popular, however rfc6902 is (to my mind) the most compelling because it will interoperate with other libraries that support RFC6902 standard.

However ... the patch formats used by these modules tends to be cryptic and overly verbose - a list of the mutations needed to transform between the two states. In the case of deep-diff you end up with this patch:

console.log(deepPatch);

⇒ [
⇒   {
⇒     kind: 'E',
⇒     path: [ 'name' ],
⇒     lhs: 'my object',
⇒     rhs: 'updated object'
⇒   },
⇒   { kind: 'D', path: [ 'description' ], lhs: "it's an object!" },
⇒   {
⇒     kind: 'A',
⇒     path: [ 'details', 'with' ],
⇒     index: 4,
⇒     item: { kind: 'N', rhs: [ [Function: Object] ] }
⇒   },
⇒   {
⇒     kind: 'A',
⇒     path: [ 'details', 'with' ],
⇒     index: 3,
⇒     item: { kind: 'N', rhs: 'elements' }
⇒   },
⇒   {
⇒     kind: 'E',
⇒     path: [ 'details', 'with', 2 ],
⇒     lhs: 'elements',
⇒     rhs: 'more'
⇒   },
⇒   { kind: 'N', path: [ 'title' ], rhs: "it's an object!" }
⇒ ]

And for rfc6902:

console.log(rfcPatch);

⇒ [
⇒   { op: 'remove', path: '/description' },
⇒   { op: 'add', path: '/title', value: "it's an object!" },
⇒   { op: 'replace', path: '/name', value: 'updated object' },
⇒   { op: 'add', path: '/details/with/2', value: 'more' },
⇒   { op: 'add', path: '/details/with/-', value: { than: 'before' } }
⇒ ]

The advantage(?) of this module is that the patch structure mirrors the structure of the target data. As such, it terse, readable, and resilient.

That said, this module may not be for everyone. In particular, readers may find the DROP and KEEP values (described below) to be... "interesting".

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.value(val)

Normalize patch values. Currently this just converts DROP values to undefined, otherwise returns the value. This is useful in determining if a patch has a meaningful value. E.g.

const newPatch = {foo: jsondiff.DROP, bar: 123};

newPatch.foo; // ⇨ '-'
jsondiff.value(newPatch.foo); // ⇨ undefined
jsondiff.value(newPatch.bar); // ⇨ 123
jsondiff.value(newPatch.whups); // ⇨ undefined

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