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-compare-advanced

v1.0.0

Published

Compare Objects, Arrays, Nested Objects/Arrays, Array of Objects.

Downloads

10

Readme

Deep Compare Objects / Arrays.

Not a typical object compare package. Compare Objects, Arrays, Nested Objects/Arrays, Array of Objects.

About

deep-compare-advanced is a object/array comparision module which helps us to compare arrays, objects, array of objects, object with arrays, and various combination of JS data structures within the object/array datatype with zero dependencies.

Features

  • Compare Objects
  • Compare Arrays
  • Compare array of objects
  • Compare deep nested arrays i.e 2D/3D arrays
  • Compare nested object with arrays
  • Get the exact path of the values that mismatch
  • Get the exact values that mismatch

Installation

First install Node.js Then:

Using npm:

$ npm install deep-compare-advanced

Using pnpm:

$ pnpm install deep-compare-advanced

Importing

// Using Node.js `require()`
const { deepCompare } = require("deep-compare-advanced");

// Using ES6 imports
import { deepCompare } from "deep-compare-advanced";

Example

deepCompare(datatype1, datatype2, strictCheck(optional), datatypeName1(optional), datatypeName2(optional));

Params

  • datatype1 / datatype2 can be object or array.
  • (Optional) strictCheck (Boolean) by default false is used for strict checking of arrays index elements. if true strict check of ordering of the array elements. if false the array values can be in any order.
  • (Optional) datatypeName1 / datatypeName2 (string) these two fields represent the name of the object/array, which is displayed in the result object in case of error. by default values for objects are Object1, Object2, and for arrays are Array1, Array2.

Return

  // In case of objects/arrays match
  {
     status: true,
     result: {},
  }

  // In case of object/arrays do not match
  {
      status: false,
      result: {
        pathOne: `Object1.data1`,
        valueOne: ["two", "one", "three", 10],
        pathTwo: `Object2.data1`,
        valueTwo: ["one", "two", "three"],
        reason: `Array length does not match.`,
      },
    }
  • status (Boolean) contains the comparision result.
  • result (object) contains all the details about the mismatch if status is false.
  • pathOne / pathTwo (string) contains the exact path of the values that mismatch.
  • valueOne / valueTwo (string) contains the values of the respective paths.
  • reason (string) contains short summary of the error.

Example 1

// Basic object / array compare.
console.log(deepCompare({ key: "value" }, { key: "value" }));
console.log(deepCompare(["value1", "value2"], ["value1", "value2"]));
// returns
// {
//     status: true,
//     result: {},
// }

console.log(deepCompare({ key: "value" }, { key: "value1" }));
// returns
// {
//   status: false,
//   result: {
//     pathOne: 'Object1.key',
//     valueOne: 'value',
//     pathTwo: 'Object2.key',
//     valueTwo: 'value1',
//     reason: 'Values mismatch path1 Object1.key value "value", path2 Object2.key value "value1".'
//   }
// }

Example 2

// array containing same values but in different order strictCheck false by default.
console.log(
  deepCompare(["value1", "value2", "value3"], ["value2", "value3", "value1"])
);
// returns
// {
//     status: true,
//     result: {},
// }

// array containing same values but in different order but this time strictCheck is true.
console.log(
  deepCompare(
    ["value1", "value2", "value3"],
    ["value2", "value3", "value1"],
    true
  )
);
// returns
// {
//   status: false,
//   result: {
//     pathOne: 'Array1[0]',
//     valueOne: 'value1',
//     pathTwo: 'Array2[0]',
//     valueTwo: 'value2',
//     reason: 'Values mismatch path1 Array1[0] path2 Array2[0].'
//   }
// }

Example 3

// Array of objects with value mismatch, shuffled order.
// passed optional params for custom array names.
// Note strictCheck is false.
console.log(
  deepCompare(
    [
      { obj1: "obj1" },
      { obj2: "obj2" },
      { obj3: { nestedObject: { data1: "data1", data2: "data2" } } },
    ],
    [
      { obj1: "obj1" },
      { obj3: { nestedObject: { data1: "data1", data2: "value" } } },
      { obj2: "obj2" },
    ],
    false,
    "MyArray1",
    "MyArray2"
  )
);
// returns
// {
//   status: false,
//   result: {
//     pathOne: 'MyArray1[2].obj3.nestedObject.data2',
//     valueOne: 'data2',
//     pathTwo: 'MyArray2[0].obj3.nestedObject.data2',
//     valueTwo: 'value',
//     reason: 'Values mismatch path1 MyArray1[2].obj3.nestedObject.data2 value "data2",
//      path2 MyArray2[0].obj3.nestedObject.data2 value "value".'
//   }
// }

Example 4

// Object with random ordering and array with random ordering.
// Note strictCheck is true. strictCheck is only considered of arrays.
console.log(
  deepCompare(
    {
      check: {
        display: {
          bool: true,
          array: ["one", "two", { testObj: "value" }],
        },
      },
      foo: {
        test: "hello",
      },
    },
    {
      foo: {
        test: "hello",
      },
      check: {
        display: {
          array: ["one", { testObj: "value" }, "two"],
          bool: true,
        },
      },
    },
    true
  )
);

// returns
// {
//   status: false,
//   result: {
//     pathOne: 'Object1.check.display.array[1]',
//     valueOne: 'two',
//     pathTwo: 'Object2.check.display.array[1]',
//     valueTwo: { testObj: 'value' },
//     reason: 'Values mismatch path1 Object1.check.display.array[1] 
//       path2 Object2.check.display.array[1].'
//   }
// }

License

MIT © Saurabh Nimkande