Created
December 21, 2016 22:06
-
-
Save gmmorris/f5ed19f93e3933f10de5ac9cd86c3389 to your computer and use it in GitHub Desktop.
Diffing of plain objects, assumes lodash
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
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