Skip to content

Instantly share code, notes, and snippets.

@gmmorris
Created December 21, 2016 22:06
Show Gist options
  • Save gmmorris/f5ed19f93e3933f10de5ac9cd86c3389 to your computer and use it in GitHub Desktop.
Save gmmorris/f5ed19f93e3933f10de5ac9cd86c3389 to your computer and use it in GitHub Desktop.
Diffing of plain objects, assumes lodash
function diffObjects (left, right) {
const leftKeys = Object.keys(left)
const rightKeys = Object.keys(right)
const keyDiff = _.intersection(leftKeys, rightKeys)
.reduce((differentKeys, key) => {
if(_.isPlainObject(left[key]) && _.isPlainObject(right[key])) {
const diff = diffObjects(left[key], right[key])
if(diff !== true) {
differentKeys.push(key)
}
} else if(Array.isArray(left[key]) && Array.isArray(right[key])) {
if(_.difference(left[key], right[key]).length) {
differentKeys.push(key)
}
} else if (left[key] !== right[key]) {
differentKeys.push(key)
}
return differentKeys
}, _.difference(leftKeys, rightKeys))
return keyDiff.length ? keyDiff : true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment