Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. // Only dependency is the lodash library
  2.  
  3. function deepDiff(oldObj, newObj) {
  4. // Instantiate our changes obj
  5. var changes = {};
  6.  
  7. // Make sure we are working with objects
  8. oldObj = _(oldObj).value();
  9. newObj = _(newObj).value();
  10.  
  11. function merge(oldObj, newObj, parent) {
  12. // Attempt to merge old and new objects, and highjack the values in a callback
  13. _.merge(oldObj, newObj, function (oldValue, newValue, key) {
  14. // Make sure we are dealing with different values
  15. if (!_.isEqual(oldValue, newValue)) {
  16. // If current value is an object, we need to set instantiate a new object
  17. // and recurse through the changed object's children
  18. if (_.isObject(newValue)) {
  19. parent[key] = {};
  20. merge(oldValue, newValue, parent[key]);
  21. } else {
  22. // Otherwise we set the value
  23. parent[key] = newValue;
  24. }
  25. }
  26. });
  27. }
  28.  
  29. // Start it off
  30. merge(oldObj, newObj, changes);
  31.  
  32. return changes;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement