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

deep-object-diff-array

v1.2.3

Published

Deep diffs two objects, including nested structures of arrays and objects, and return the difference.

Downloads

194

Readme

❄️

Deep diff two JavaScript Objects

Build Status Code Coverage version downloads MIT License PRs Welcome

A small library that can deep diff two JavaScript Objects, including nested structures of arrays and objects.

Installation

yarn add deep-object-diff-array

Functions available:

Importing

ES6 / Babel:

import { addedDiff, deletedDiff, updatedDiff, detailedDiff } from 'deep-object-diff-array';

ES5:

const { addedDiff, deletedDiff, detailedDiff, updatedDiff } = require("deep-object-diff-array");

// OR

const addedDiff = require("deep-object-diff-array").addedDiff;
const deletedDiff = require("deep-object-diff-array").deletedDiff;
const detailedDiff = require("deep-object-diff-array").detailedDiff;
const updatedDiff = require("deep-object-diff-array").updatedDiff;

Usage:

Set flag:

Set means consider array element is unique and the order doesn't matter (updatedDiff array always empty in Set)

addedDiff:

const lhs = {
  foo: {
    bar: {
      a: ['a', 'b'],
      b: 2,
      c: ['x', 'y'],
      e: 100 // deleted
    }
  },
  buzz: 'world'
};

const rhs = {
  foo: {
    bar: {
      a: ['a'], // index 1 ('b')  deleted
      b: 2, // unchanged
      c: ['y', 'x', 'z'], // 'z' added and reordered x, y
      d: 'Hello, world!' // added
    }
  },
  buzz: 'fizz' // updated
};

console.log(addedDiff(lhs, rhs));

/*
{
  "foo": {
    "bar": {
      "c": {
        "after": [
          "z"
        ]
      },
      "d": {
        "after": "Hello, world!"
      }
    }
  }
}
*/

console.log(addedDiff(lhs, rhs, false)); // non Set
/*
{
  "foo": {
    "bar": {
      "c": {
        "after": [
          {
            "content": "z",
            "indices": [
              2 // element added in index 2 of array
            ]
          }
        ]
      },
      "d": {
        "after": "Hello, world!" // normal add new field in object
      }
    }
  }
}
*/

deletedDiff:

const lhs = {
  foo: {
    bar: {
      a: ['a', 'b'],
      b: 2,
      c: ['x', 'y'],
      e: 100 // deleted
    }
  },
  buzz: 'world'
};

const rhs = {
  foo: {
    bar: {
      a: ['a'], // index 1 ('b')  deleted
      b: 2, // unchanged
      c: ['y', 'x', 'z'], // 'z' added and reordered x, y
      d: 'Hello, world!' // added
    }
  },
  buzz: 'fizz' // updated
};

console.log(deletedDiff(lhs, rhs));

/*
{
  "foo": {
    "bar": {
      "a": {
        "before": [
          "b"
        ]
      },
      "e": {
        "before": 100
      }
    }
  }
}
*/

console.log(deletedDiff(lhs, rhs, false));

/*
{
  "foo": {
    "bar": {
      "a": {
        "before": [
          {
            "content": "b",
            "indices": [
              1 // removed element from array index 1
            ]
          }
        ]
      },
      "e": {
        "before": 100 // normal remove field in object
      }
    }
  }
}
*/

updatedDiff:

const lhs = {
  foo: {
    bar: {
      a: ['a', 'b'],
      b: 2,
      c: ['x', 'y'],
      e: 100 // deleted
    }
  },
  buzz: 'world'
};

const rhs = {
  foo: {
    bar: {
      a: ['a'], // index 1 ('b')  deleted
      b: 2, // unchanged
      c: ['y', 'x', 'z'], // 'z' added and reordered x, y
      d: 'Hello, world!' // added
    }
  },
  buzz: 'fizz' // updated
};

console.log(updatedDiff(lhs, rhs));

/*
{
  "buzz": {
    "before": "world",
    "after": "fizz"
  }
}
*/

console.log(updatedDiff(lhs, rhs, false));

/*
{
  "foo": {
    "bar": {
      "c": {
        "after": [ // first element x and first element y swapped
          {
            "content": "y",
            "counter": 0,
            "newIndex": 0,
            "oldIndex": 1
          },
          {
            "content": "x",
            "counter": 0,
            "newIndex": 1,
            "oldIndex": 0
          }
        ]
      }
    }
  },
  "buzz": {
    "before": "world",
    "after": "fizz"
  }
}
*/

detailedDiff:

const lhs = {
  foo: {
    bar: {
      a: ['a', 'b'],
      b: 2,
      c: ['x', 'y'],
      e: 100 // deleted
    }
  },
  buzz: 'world'
};

const rhs = {
  foo: {
    bar: {
      a: ['a'], // index 1 ('b')  deleted
      b: 2, // unchanged
      c: ['y', 'x', 'z'], // 'z' added and reordered x, y
      d: 'Hello, world!' // added
    }
  },
  buzz: 'fizz' // updated
};

console.log(detailedDiff(lhs, rhs));

/*
{
  "added": {
    "foo": {
      "bar": {
        "c": {
          "after": [
            "z"
          ]
        },
        "d": {
          "after": "Hello, world!"
        }
      }
    }
  },
  "deleted": {
    "foo": {
      "bar": {
        "a": {
          "before": [
            "b"
          ]
        },
        "e": {
          "before": 100
        }
      }
    }
  },
  "updated": {
    "buzz": {
      "before": "world",
      "after": "fizz"
    }
  }
}
*/

console.log(detailedDiff(lhs, rhs, false));
/*
{
  "added": {
    "foo": {
      "bar": {
        "c": {
          "after": [
            {
              "content": "z",
              "indices": [
                2
              ]
            }
          ]
        },
        "d": {
          "after": "Hello, world!"
        }
      }
    }
  },
  "deleted": {
    "foo": {
      "bar": {
        "a": {
          "before": [
            {
              "content": "b",
              "indices": [
                1
              ]
            }
          ]
        },
        "e": {
          "before": 100
        }
      }
    }
  },
  "updated": {
    "foo": {
      "bar": {
        "c": {
          "after": [
            {
              "content": "y",
              "counter": 0,
              "newIndex": 0,
              "oldIndex": 1
            },
            {
              "content": "x",
              "counter": 0,
              "newIndex": 1,
              "oldIndex": 0
            }
          ]
        }
      }
    },
    "buzz": {
      "before": "world",
      "after": "fizz"
    }
  }
}
*/

License

MIT