Skip to content

Instantly share code, notes, and snippets.

@Xexuline
Last active November 14, 2019 09:31
Show Gist options
  • Select an option

  • Save Xexuline/9775b0f70a6e01d8b594c936b78d3795 to your computer and use it in GitHub Desktop.

Select an option

Save Xexuline/9775b0f70a6e01d8b594c936b78d3795 to your computer and use it in GitHub Desktop.
Check differences between 2 objects
// need loadsh map, reduce and isEqual
var result = {
different: [],
missing_from_first: [],
missing_from_second: [],
}
reduce(a, function (result, value, key) {
if (b.hasOwnProperty(key)) {
if (isEqual(value, b[key])) {
return result
} else {
if (typeof (a[key]) != typeof ({}) || typeof (b[key]) != typeof ({})) {
//dead end.
result.different.push(key)
return result
} else {
var deeper = compare(a[key], b[key])
result.different = result.different.concat(map(deeper.different, (sub_path) => {
return key + "." + sub_path
}))
result.missing_from_second = result.missing_from_second.concat(map(deeper.missing_from_second, (sub_path) => {
return key + "." + sub_path
}))
result.missing_from_first = result.missing_from_first.concat(map(deeper.missing_from_first, (sub_path) => {
return key + "." + sub_path
}))
return result
}
}
} else {
result.missing_from_second.push(key)
return result
}
}, result)
reduce(b, function (result, value, key) {
if (a.hasOwnProperty(key)) {
return result
} else {
result.missing_from_first.push(key);
return result
}
}, result)
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment