Skip to content

Instantly share code, notes, and snippets.

@jasongaare
Created April 10, 2018 16:02
Show Gist options
  • Save jasongaare/277d08adec172bcb4985453c291d5a88 to your computer and use it in GitHub Desktop.
Save jasongaare/277d08adec172bcb4985453c291d5a88 to your computer and use it in GitHub Desktop.
const compare = (a, b) => {
const finalResult = {
different: [],
missing_from_first: [],
missing_from_second: [],
};
_.reduce(a, (result, value, key) => {
if (b.hasOwnProperty(key)) {
if (_.isEqual(value, b[key])) {
return result;
}
if (typeof (a[key]) !== typeof ({}) || typeof (b[key]) !== typeof ({})) {
result.different.push(key);
return result;
}
const deeper = compare(a[key], b[key]);
finalResult.different = finalResult.different.concat(_.map(deeper.different, (subPath) => {
return `${key}.${subPath}`;
}));
finalResult.missing_from_second = finalResult.missing_from_second.concat(_.map(deeper.missing_from_second, (subPath) => {
return `${key}.${subPath}`;
}));
finalResult.missing_from_first = finalResult.missing_from_first.concat(_.map(deeper.missing_from_first, (subPath) => {
return `${key}.${subPath}`;
}));
return result;
}
finalResult.missing_from_second.push(key);
return result;
}, finalResult);
_.reduce(b, (result, value, key) => {
if (a.hasOwnProperty(key)) {
return result;
}
finalResult.missing_from_first.push(key);
return result;
}, finalResult);
return finalResult;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment