Skip to content

Instantly share code, notes, and snippets.

@edn-g
Created September 5, 2017 12:51
Show Gist options
  • Save edn-g/725ac104a46cca2d3baa7d6f54b72a29 to your computer and use it in GitHub Desktop.
Save edn-g/725ac104a46cca2d3baa7d6f54b72a29 to your computer and use it in GitHub Desktop.
Objects diff util
/**
* Objects diff util
* @param object a object to compare
* @param object b object to compare
* @return the difference between the two objects
*/
(function() {
odiff = function(a, b) {
var $this = this;
var result = {};
var ref = _.merge(null, a, b);
_.forEach(ref, function(value, key) {
if (_.isEqual(a[key], b[key])) {
return;
}
if (_.isObject(a[key]) && _.isObject(b[key])) {
_.forEach($this.odiff(a[key], b[key]), function(v, k) {
result[key + '.' + k] = v;
});
} else {
result[key] = [a[key], b[key]];
}
});
return result;
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment