are-objects-equals
v1.0.1
Published
A tool to compare objects easyly
Downloads
979
Readme
Are Objects Equals
Code Quality Status
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"]
}
:link: 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
*/