Last active
December 26, 2021 14:02
-
-
Save kirpalmakanga/49278036934fe68c0a5b067efa8fef43 to your computer and use it in GitHub Desktop.
Check equality between two items
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
export const isEqual = (obj1: unknown, obj2: unknown) => { | |
type GenericObject = { | |
[key: string]: unknown; | |
}; | |
function getType(obj: unknown) { | |
return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase(); | |
} | |
function areArraysEqual(arr1: unknown[], arr2: unknown[]) { | |
if (arr1.length !== arr2.length) return false; | |
for (let i = 0; i < arr1.length; i++) { | |
if (!isEqual(arr1[i], arr2[i])) return false; | |
} | |
return true; | |
} | |
function areObjectsEqual(obj1: GenericObject, obj2: GenericObject) { | |
if (Object.keys(obj1).length !== Object.keys(obj2).length) return false; | |
for (let key in obj1) { | |
if (Object.prototype.hasOwnProperty.call(obj1, key)) { | |
if (!isEqual(obj1[key], obj2[key])) return false; | |
} | |
} | |
return true; | |
} | |
function areFunctionsEqual(func1: Function, func2: Function) { | |
return func1.toString() === func2.toString(); | |
} | |
function arePrimitivesEqual(primitive1: unknown, primitive2: unknown) { | |
return primitive1 === primitive2; | |
} | |
let type = getType(obj1); | |
if (type !== getType(obj2)) return false; | |
if (type === 'array') | |
return areArraysEqual(obj1 as unknown[], obj2 as unknown[]); | |
if (type === 'object') | |
return areObjectsEqual(obj1 as GenericObject, obj2 as GenericObject); | |
if (type === 'function') | |
return areFunctionsEqual(obj1 as Function, obj2 as Function); | |
return arePrimitivesEqual(obj1, obj2); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment