Created
September 5, 2017 12:51
-
-
Save edn-g/725ac104a46cca2d3baa7d6f54b72a29 to your computer and use it in GitHub Desktop.
Objects diff util
This file contains hidden or 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
/** | |
* 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