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

small-json-diff

v1.4.6

Published

Library for creating json small RFC6902 json diffs.

Downloads

6

Readme

small-json-diff

Library for creating small RFC6902 compliant json diffs. The char length of the diff is guaranteed to be smaller than that of the diff produced by replacing the entire object. This is recursively true for each sub-object.

The algorithm will produce "add", "remove" and "replace" operations. The diff produced (limited to these operations) will be the smallest possible as long as no arrays are used. The optimal diff may not be computed when multiple changes are made inside an array. But some common scenarios are supported:

  1. One or more items gets a new value.
  2. One or more items are added to the end of the array.
  3. One or more items are removed from the end of the array.
  4. Exactly one item is added or removed at any position in the array.

1 can be combined with 2 or 3. In all of these cases the optimal diff will be produced.

Install

Install the current version (and save it as a dependency):

$ npm install small-json-diff --save

Usage

var smallJsonDiff = require("small-json-diff");

var oldDocument = {
    type: "Test document",
    arrayWillChange: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    numberWillChange: 1,
    prop1: 1,
    prop2: {
        test1: 123,
        test2: 456
    },
    prop3: null
};

var newDocument = {
    type: "Test document",
    arrayWillChange: [1, 2, 3, 4, 5],
    numberWillChange: 2,
    prop1: 1,
    prop2: {
        test1: 123,
        test2: 456
    },
    prop3: null
};

var diff = smallJsonDiff.diff(oldDocument, newDocument);

// diff = [
//   {
//     "op": "replace",
//     "path": "/numberWillChange",
//     "value": 2
//   },
//   {
//     "op": "replace",
//     "path": "/arrayWillChange",
//     "value": [1, 2, 3, 4, 5]
//   }
// ]

The diff can be used as input to a patch operation for any RFC6902 compliant library. For instance could the fast-json-patch library be used like this:

var fastJsonPatch = require("fast-json-patch");

var newDocument = fastJsonPatch.applyPatch(oldDocument, diff);

Typescript

This library is written in Typescript and the Typescript definitions are included.

Complexity

The complexity of the diff funciton is O(n) where n is the number of unique paths in the objects that are diffed.

Main logic (simplified)

The paths in the old object and new object are traversed. During this traversal the information needed to create a small diff is gathered and stored in a tree structure. For each node the correct diff operation is determined. There are 5 possible operations: "add", "remove", "replace", "children" and "none.

  • "add" = Perform add operation for this node's path with the value at this path in the new object.

  • "remove" = Perform remove operation for this node's path.

  • "replace" = Perform replace operation for this node's path with the value at this path in the new object.

  • "none" = Do nothing. The values match.

  • "children" = Perform the operations dictated by this node's children

The length of the diff required to perform the operation is determined and stored as "diffSize". When the operation is "children" the diffSize is the sum of the children's diffSizes.

When determining what operation should be performed the diffSize of using "children" is compared to the diffSize of performing a "replace".

Arrays are scanned in such a way that add/remove of 1 element will be detected.