1.0.3 • Published 4 years ago
strict-deep-equal v1.0.3
strict deep equal
strict and deep equality check for any combination of simple values, objects or arrays
fastest implementation of strict deep equality. Use strict deep equal if you want to check if your data has been mutated in any way.
Installation
npm i strict-deep-equal
Usage
const strictDeepEqual = require('strict-deep-equal')
strictDeepEqual( value1, value2 )
// returns true, if they are equal
// returns false if not equal
Examples
Even when 2 objects are same, if their ids are in different order they are considered not equal
const a = { id: 'some id', age: 20 }
const b = { id: 'some id', age: 20 }
const c = { age: 20, id: 'some id', }
strictDeepEqual( a, b ) // true
strictDeepEqual( a, c ) // false
equality is checked deeply
const a = {
x: [ 1, 'string', 10 ],
y: [ { z: 'other string', w: { x: 2000}, e: true, f: null } ],
z: null,
p: [ {}, [], [ [ [] ] ] ]
}
// same as above
const b = {
x: [ 1, 'string', 10 ],
y: [ { z: 'other string', w: { x: 2000}, e: true, f: null } ],
z: null,
p: [ {}, [], [ [ [] ] ] ]
}
strictDeepEqual(a, b) // true
// changed at --> *
const c = {
x: [ 1, 'string', 11 /*changed here*/ ],
y: [ { z: 'other string', w: { x: 2000}, e: true, f: null } ],
z: null,
p: [ {}, [], [ [ [] ] ] ]
}
strictDeepEqual(a, c) // false
Note
- strict-deep-equal assumes that you will only be giving either simple values, or any combination of objects or arrays.
- It does not compare functions, sets, regex, or any other.
Where should you use this ?
- When you are dealing with serializable data and want to check if the 2 data values are same or not
- you can also check if a certain data value is mutated or not after calling some function. You can do this by first cloning the given data using any deep-clone function. then calling the said function and then checking the data with the cloned data for mutation using strict-deep-equal
Author
Manan Tank