Created
March 17, 2022 20:06
-
-
Save tannerlinsley/1b9e709a502fc8c56bc9dbd2728864c3 to your computer and use it in GitHub Desktop.
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 function deepDiff<TObj extends object>(a: TObj, b: TObj) { | |
if (Array.isArray(a)) { | |
const changes = a | |
.map((item, index) => deepDiff(item, b[index])) | |
.filter(Boolean) | |
return changes.length ? changes : undefined | |
} | |
if (isObject(a)) { | |
const changes = {} | |
Object.keys(a).forEach(key => { | |
const diff = deepDiff(a[key], b[key]) | |
if (diff) { | |
changes[key] = diff | |
} | |
}) | |
return Object.keys(changes).length ? changes : undefined | |
} | |
return a !== b ? ([a, b] as const) : undefined | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment