are-objects-equals

1.0.1 • Public • Published

Are Objects Equals

Code Quality Status

Build Status Coverage Status

npm-are-objects-equals

Description

A Function to compare and normalize objects easier

Installation

npm i are-objects-equals

Params

areObjectsEquals(objectBase, objectToCompare, options)

objectBase and objectToCompare

  • Items to compare
  • Type: Object
  • Required

Example

{
    name: "Juan Román",
    lastname: "Riquelme",
    clubs: ["Boca", "Barcelona", "Villareal", "Argentinos"],
    number: 10,
    stillPlaying: false,
    birthPlace: { country: "Argentina", province: "Buenos Aires", city: "Don Torcuato" }
}

options

  • Options to normalize the objects before compare
  • Type: Object
  • Optional

Example

{
    fieldsToKeep: ["name", "number"]
}

🔗 See more in Object Normalize

Usage

areObjectsEquals(objectBase, objectToCompare)

Will compare the 2 objects without any formatting

const areObjectsEquals = require('are-objects-equals');

const playerSample1 = {
    name: "Juan Román",
    lastname: "Riquelme",
    clubs: ["Boca", "Barcelona", "Villareal", "Argentinos"],
    number: 10,
    stillPlaying: false,
    birthPlace: { country: "Argentina", province: "Buenos Aires", city: "Don Torcuato" }
};

const playerSample2 = {
    name: "Juan Román",
    lastname: "Riquelme",
    clubs: ["Boca", "Barcelona", "Villareal", "Argentinos"],
    number: 10
}

areObjectsEquals(playerSample1, playerSample2);

/*
output: false
*/

areObjectsEquals(playerSample1, playerSample1);

/*
output: true
*/

areObjectsEquals(objectBase, objectToCompare, options)

Will compare objects after normalize them

const areObjectsEquals = require('are-objects-equals');

const playerSample1 = {
    name: "Juan Román",
    lastname: "Riquelme",
    clubs: ["Boca", "Barcelona", "Villareal", "Argentinos"],
    number: 10,
    stillPlaying: false,
    birthPlace: { country: "Argentina", province: "Buenos Aires", city: "Don Torcuato" }
};

const playerSample2 = {
    name: "Juan Román",
    lastname: "Riquelme",
    clubs: ["Boca", "Barcelona", "Villareal", "Argentinos"],
    number: 10
}

areObjectsEquals(playerSample1, playerSample2, { fieldsToKeep: ["name", "lastname", "clubs", "number"]});

/*
output: true
*/

const playerSample3 = {
    name: "Lionel",
    lastname: "Messi",
    clubs: ["Barcelona"],
    number: 10,
    stillPlaying: true
}

areObjectsEquals(playerSample1, playerSample3, { fieldsToRemove: ["name", "lastname", "clubs", "stillPlaying"]});

/*
output: true
*/

Readme

Keywords

none

Package Sidebar

Install

npm i are-objects-equals

Weekly Downloads

434

Version

1.0.1

License

MIT

Unpacked Size

6.42 kB

Total Files

5

Last publish

Collaborators

  • gaston.pereyra