-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from EveripediaNetwork/is-deep-equal
done adding logic for deep comparison
- Loading branch information
Showing
3 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@everipedia/iq-utils': patch | ||
--- | ||
|
||
Create lib for deep comparison |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
function isDeepEqual(obj1: any, obj2: any): boolean { | ||
// If both are strings, compare them ignoring whitespace | ||
if (typeof obj1 === 'string' && typeof obj2 === 'string') { | ||
return obj1.replace(/\s+/g, '') === obj2.replace(/\s+/g, ''); | ||
} | ||
|
||
// Check if they are the same reference or both null/undefined | ||
if (obj1 === obj2) { | ||
return true; | ||
} | ||
|
||
// Check if either is null/undefined, or not both are objects (including functions) | ||
if ( | ||
typeof obj1 !== 'object' || | ||
obj1 === null || | ||
typeof obj2 !== 'object' || | ||
obj2 === null | ||
) { | ||
return false; | ||
} | ||
|
||
// Get the keys of both objects | ||
const keysA = Object.keys(obj1); | ||
const keysB = Object.keys(obj2); | ||
|
||
// Check if the number of properties is different | ||
if (keysA.length !== keysB.length) { | ||
return false; | ||
} | ||
|
||
// Check if obj2 has all keys present in obj1 and compare each key | ||
for (const key of keysA) { | ||
if (!keysB.includes(key)) { | ||
return false; | ||
} | ||
|
||
// Check if the property is a function and compare the function's code | ||
if (typeof obj1[key] === 'function' || typeof obj2[key] === 'function') { | ||
if (obj1[key].toString() !== obj2[key].toString()) { | ||
return false; | ||
} | ||
} else { | ||
// Recursive call for nested objects or arrays | ||
if (!isDeepEqual(obj1[key], obj2[key])) { | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
export default isDeepEqual; |