Skip to content

Instantly share code, notes, and snippets.

@tannerlinsley
Created March 17, 2022 20:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tannerlinsley/1b9e709a502fc8c56bc9dbd2728864c3 to your computer and use it in GitHub Desktop.
Save tannerlinsley/1b9e709a502fc8c56bc9dbd2728864c3 to your computer and use it in GitHub Desktop.
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