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

json-patch-gen

v1.0.2

Published

JavaScript library that generates JSON patches (RFC 6902)

Downloads

9,552

Readme

Introduction

json-patch-gen is a small, [thoroughly tested] tests library for generating a [JSON Patch] rfc6902 from two JavaScript objects or arrays.

This library has no dependencies. It supports generating patches of arbitrarily nested objects and arrays to any depth.

json-patch-gen does not apply patches. There are several libraries that can perform this for you:

If you wish to modify an object and then generate a patch from the modifications just performed, [this library] starcounter may work better for you. It uses an observer to achieve this. Json-patch-gen takes a different approach: it recursively compares the two values given.

It appears that rfc6902 can also generate patches. I missed this while researching a solution to this problem. I've not compared the projects. There is room for different approaches to generating patches so I suggest trying both libraries to see which works better for you.

Installation

npm install json-patch-gen --save

or

bower install json-patch-gen

Once installed, the module exports a single function that takes two values to diff.

var diff = require('json-patch-gen');

You can also use this library in the browser by loading with a script tag. A single 'diff' function will be exported to the global scope.

Examples

Here is a simple example repl session:

> var diff = require('json-patch-gen');
undefined

> diff({},{});
[]

> diff([],[1])
[ { op: 'add', path: '/0', value: 1 } ]

> diff({foo: 'bar'}, {foo: 'baz'})
[ { op: 'replace', path: '/foo', value: 'baz' } ]

> diff({foo: 'bar'}, {})
[ { op: 'remove', path: '/foo' } ]

> diff({}, {foo: 'bar'})
[ { op: 'add', path: '/foo', value: 'bar' } ]

> diff({foo: {bar: 'baz'}}, {foo: {bar: 'quux'}})
[ { op: 'replace', path: '/foo/bar', value: 'quux' } ]

> diff([1,2,3], [2,3,4])
[ { op: 'add', path: '/3', value: 4 },
  { op: 'remove', path: '/0' } ]

> diff([{foo: 'bar'},3], [{foo: 'baz'},3,4])
[ { op: 'add', path: '/2', value: 4 },
  { op: 'replace', path: '/0/foo', value: 'baz' } ]

> diff({foo: {bar: [1,2,3]}}, {foo: {bar: [2,3,4]}})
[ { op: 'add', path: '/foo/bar/3', value: 4 },
  { op: 'remove', path: '/foo/bar/0' } ]