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

fogsaadiff

v1.0.0

Published

A json-patch diff that is intended to work well between different versions of immutable data structures. Builds on work from JSON8, and the FOGSAA algorithm. For convenience, re-exports immutable-json-patch's patch.

Downloads

2

Readme

fogsaadiff

I created this library because of frustration that typical diffs didn't work very well on arrays, e.g. when you insert or delete a value, often the diff will say that every value after it has changed.

JSON Patch defines a JSON document structure for expressing a sequence of operations to apply to a JavaScript Object Notation (JSON) document; it is suitable for use with the HTTP PATCH method.

fogsaadiff creates a JSON Patch document by diffing two JSON objects.

I have created it with the intention that it would be a good diff for working with different versions of a single immutable datastructure, so most of the deeply nested objects would be structurally shared and refer to the same objects.

Because of this, I look for opportunities to return early, and I try to avoid replacing whole objects. I also attempt to find an optimal alignment for any arrays that keeps the number of insertions and deletions small.

Implementation

The main body of the diff has been modified from the diff implemented in JSON8.

The array part of the diff is an alignment made by the fogsaa algorithm. It has been ported from C code that I found in github by Hanaa Mohamed.

I re-export the patch from immutable-json-patch, because that is likely to be the most appropriate patch function to use for use cases where this diff is useful.

Example

const bob = {name: 'bob', email: '[email protected]', age: 32, address: {no: 52, street: "state st"}}
const toby = {name: 'toby', email: '[email protected]', age: 27, address: {no: 33, street: "mine close"}}
const jim = {name: 'jim', email: '[email protected]', age: 11, address: {no: 1, street: "world"}}
const alf = {name: 'alf', email: '[email protected]', age: 15, address: {no: 3, street: "leon st"}}
const toby2 = {name: 'toby', email: '[email protected]', age: 17, address: {no: 33, street: "mine close"}}

const a = [toby, bob, jim]
const b = [alf, toby2, bob, jim]

const p = diff(a, b)
const r = patch(a, p)

// The diff & patching process has not changed any items in a that didn't need to be changed.
console.log(a[1] === r[2], a[2] === r[3], r[0] === alf, r[2] === bob, r[3] === jim, r[1].address === toby.address)

The patch that fogsadiff identifies looks like this:

[
  {
    "op": "replace",
    "path": "/0/age",
    "value": 17
  },
  {
    "op": "add",
    "path": "/0",
    "value": {
      "name": "alf",
      "email": "[email protected]",
      "age": 15,
      "address": {
        "no": 3,
        "street": "leon st"
      }
    }
  }
]

Notice that it's spotted that there's been a single insertion and a single edit, and the other values in the array haven't changed at all. JSON8 diff will create a patch that replaces every item in the array with a new one, with 44 lines of patch, instead of 19.