1.1.7 • Published 4 months ago

nested-object-compare v1.1.7

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

Important: Version 1.1.4 fixes a critical issue with importing. Please upgrade to the latest version to avoid potential errors.

deep-object-compare

A robust utility for deeply comparing objects and providing detailed difference information. Handles nested objects and arrays.

Installation

npm install nested-object-compare

Usage

Basic Comparison

const { deepCompare } = require('nested-object-compare');

const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { a: 1, b: { c: 2 } };

const result = deepCompare(obj1, obj2);
console.log(result);
// Output: { equal: true }

Detecting Differences

const obj3 = { a: 1, b: { c: 2, d: [3, 4] } };
const obj4 = { a: 1, b: { c: 2, d: [3, 5] } };

const result2 = deepCompare(obj3, obj4);
console.log(result2);
/* Output:
{
  equal: false,
  differences: []
}
*/

Verbose Mode(Structured Path)

const resultVerbose = deepCompare(obj3, obj4, { verbose: true });
console.log(resultVerbose);
/* Output:
{
  equal: false,
  differences: [
    { path: '["b"]["d"][1]', value1: 4, value2: 5, message: 'Values differ: 4 vs 5' }
  ]
}
*/

Verbose Mode(Dot Notation Path)

const resultVerbose = deepCompare(obj3, obj4, { verbose: true,pathFormat: "dot" });
console.log(resultVerbose);
/* Output:
{
  equal: false,
  differences: [
    { path: "b.d.1", value1: 4, value2: 5, message: 'Values differ: 4 vs 5' }
  ]
}
*/

Strict vs. Loose Comparison

const obj5 = { a: 1, b: { c: 2 } };
const obj6 = { a: 1, b: { c: '2' } }; // '2' is a string

const result3 = deepCompare(obj5, obj6); // Strict (default)
console.log(result3);
/* Output:
{
  equal: false,
  differences: []
}
*/

const result4 = deepCompare(obj5, obj6, { strict: false }); // Loose
console.log(result4);
/* Output:
 { equal: true,
// differences: []
}
*/
1.1.7

4 months ago

1.1.6

4 months ago

1.1.5

4 months ago

1.1.4

4 months ago

1.1.3

4 months ago

1.1.2

4 months ago

1.1.11

4 months ago

1.1.1

4 months ago

1.0.9

4 months ago

1.0.8

4 months ago

1.0.7

4 months ago

1.0.6

4 months ago

1.0.5

4 months ago

1.0.3

4 months ago

1.0.2

4 months ago

1.0.1

4 months ago

1.0.0

4 months ago