Check whether objects are equal in deep. Useful for checking options objects
npm install is-equal-objects bash
$ npm install is-equal-objects --save
`
` bash
$ yarn add is-equal-objects
`
``javascript
import isEqualObjects from 'is-equal-objects/esm';
//OR
const isEqualObjects= require('is-equal-objects');
``
CDN
Use unpkg.com cdn to get the link to the script/module from the package:
- minified UMD ES5 version (~1kB)
`html
`
- ESM ES2015 module(~2kB)
`javascript
import {isEqualObjects, cloneObject} from "https://unpkg.com/is-equal-objects/dist/is-equal-objects.esm.js"
//or minified version
import {isEqualObjects, cloneObject} from "https://unpkg.com/is-equal-objects/dist/is-equal-objects.esm.min.js"
`
Usage examples
Basic usage:
``javascript
isEqualObjects(1, 1); //true
isEqualObjects({x:1}, {x:1}); //true
isEqualObjects({x:1}, {x:1}, {x:1}); //true
isEqualObjects([{x:1}], [{x:1}]); //true
isEqualObjects([{x:[1]}], [{x:[1]}]); //true
const complexObj= Object.create({});
Object.assign(complexObj, {x:1});
isEqualObjects({x:1}, complexObj); //false, complexObj is not a flat object, test by reference
isEqualObjects(new Date(1995, 11, 17), new Date(1995, 11, 17)); //true
isEqualObjects(/\s+/, /\s+/); //true
isEqualObjects(
Object.create({
[isEqualObjects.plainObject]: true,
x: 123
}),
Object.create({
[isEqualObjects.plainObject]: true,
x: 123
})
); // true
isEqualObjects.call({
comparator(obj1, obj2){
return obj1.x === obj2.x;
}
}, {x: 123, ignoredProp: 456}, {x:123, ignoredProp: 987});
// cloneObject usage
const sourceObj= {x: {y: new Date()}};
const clonedObject = cloneObject(sourceObj);
console.log(sourceObj.x === clonedObj.x); // false
console.log(sourceObj.x.y === clonedObj.x.y); // false
console.log(isEqualObjects(sourceObj, clonedObject)); // true
``
API
$3
- ...objects objects to check
returns false, if the objects are not deeply equal, true otherwise
Additional options can be passed via function context (this):
- comparator(obj1: any, obj2: any): boolean|undefined - a custom object comparator
$3
- obj` any object to clone