Guest User

Untitled

a guest
Jun 29th, 2018
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. function findDifferences(objectA, objectB) {
  2. var propertyChanges = [];
  3. var objectGraphPath = ["this"];
  4. (function(a, b) {
  5. if(a.constructor == Array) {
  6. // BIG assumptions here: That both arrays are same length, that
  7. // the members of those arrays are _essentially_ the same, and
  8. // that those array members are in the same order...
  9. for(var i = 0; i < a.length; i++) {
  10. objectGraphPath.push("[" + i.toString() + "]");
  11. arguments.callee(a[i], b[i]);
  12. objectGraphPath.pop();
  13. }
  14. } else if(a.constructor == Object || (a.constructor != Number &&
  15. a.constructor != String && a.constructor != Date &&
  16. a.constructor != RegExp && a.constructor != Function &&
  17. a.constructor != Boolean)) {
  18. // we can safely assume that the objects have the
  19. // same property lists, else why compare them?
  20. for(var property in a) {
  21. objectGraphPath.push(("." + property));
  22. if(a[property].constructor != Function) {
  23. arguments.callee(a[property], b[property]);
  24. }
  25. objectGraphPath.pop();
  26. }
  27. } else if(a.constructor != Function) { // filter out functions
  28. if(a != b) {
  29. propertyChanges.push({ "Property": objectGraphPath.join(""), "ObjectA": a, "ObjectB": b });
  30. }
  31. }
  32. })(objectA, objectB);
  33. return propertyChanges;
  34. }
  35.  
  36. var person1 = {
  37. FirstName : "John",
  38. LastName : "Doh",
  39. Age : 30,
  40. EMailAddresses : [
  41. "john.doe@gmail.com",
  42. "jd@initials.com"
  43. ],
  44. Children : [
  45. {
  46. FirstName : "Sara",
  47. LastName : "Doe",
  48. Age : 2
  49. }, {
  50. FirstName : "Beth",
  51. LastName : "Doe",
  52. Age : 5
  53. }
  54. ]
  55. };
  56.  
  57. var person2 = {
  58. FirstName : "John",
  59. LastName : "Doe",
  60. Age : 33,
  61. EMailAddresses : [
  62. "john.doe@gmail.com",
  63. "jdoe@hotmail.com"
  64. ],
  65. Children : [
  66. {
  67. FirstName : "Sara",
  68. LastName : "Doe",
  69. Age : 3
  70. }, {
  71. FirstName : "Bethany",
  72. LastName : "Doe",
  73. Age : 5
  74. }
  75. ]
  76. };
  77.  
  78. var differences = findDifferences(person1, person2);
  79.  
  80. [
  81. {
  82. "Property":"this.LastName",
  83. "ObjectA":"Doh",
  84. "ObjectB":"Doe"
  85. }, {
  86. "Property":"this.Age",
  87. "ObjectA":30,
  88. "ObjectB":33
  89. }, {
  90. "Property":"this.EMailAddresses[1]",
  91. "ObjectA":"jd@initials.com",
  92. "ObjectB":"jdoe@hotmail.com"
  93. }, {
  94. "Property":"this.Children[0].Age",
  95. "ObjectA":2,
  96. "ObjectB":3
  97. }, {
  98. "Property":"this.Children[1].FirstName",
  99. "ObjectA":"Beth",
  100. "ObjectB":"Bethany"
  101. }
  102. ]
  103.  
  104. // Given two objects find the first key or value not matching, algorithm is a
  105. // inspired by of _.isEqual.
  106. function diffObjects(a, b) {
  107. console.info("---> diffObjects", {"a": a, "b": b});
  108. // Check object identity.
  109. if (a === b) return true;
  110. // Different types?
  111. var atype = typeof(a), btype = typeof(b);
  112. if (atype != btype) {
  113. console.info("Type mismatch:", {"a": a, "b": b});
  114. return false;
  115. };
  116. // Basic equality test (watch out for coercions).
  117. if (a == b) return true;
  118. // One is falsy and the other truthy.
  119. if ((!a && b) || (a && !b)) {
  120. console.info("One is falsy and the other truthy:", {"a": a, "b": b});
  121. return false;
  122. }
  123. // Unwrap any wrapped objects.
  124. if (a._chain) a = a._wrapped;
  125. if (b._chain) b = b._wrapped;
  126. // One of them implements an isEqual()?
  127. if (a.isEqual) return a.isEqual(b);
  128. // Check dates' integer values.
  129. if (_.isDate(a) && _.isDate(b)) return a.getTime() === b.getTime();
  130. // Both are NaN?
  131. if (_.isNaN(a) && _.isNaN(b)) {
  132. console.info("Both are NaN?:", {"a": a, "b": b});
  133. return false;
  134. }
  135. // Compare regular expressions.
  136. if (_.isRegExp(a) && _.isRegExp(b))
  137. return a.source === b.source &&
  138. a.global === b.global &&
  139. a.ignoreCase === b.ignoreCase &&
  140. a.multiline === b.multiline;
  141. // If a is not an object by this point, we can't handle it.
  142. if (atype !== 'object') {
  143. console.info("a is not an object:", {"a": a});
  144. return false;
  145. }
  146. // Check for different array lengths before comparing contents.
  147. if (a.length && (a.length !== b.length)) {
  148. console.info("Arrays are of different length:", {"a": a, "b": b});
  149. return false;
  150. }
  151. // Nothing else worked, deep compare the contents.
  152. var aKeys = _.keys(a), bKeys = _.keys(b);
  153. // Different object sizes?
  154. if (aKeys.length != bKeys.length) {
  155. console.info("Different object sizes:", {"a": a, "b": b});
  156. return false;
  157. }
  158. // Recursive comparison of contents.
  159. for (var key in a) if (!(key in b) || !diffObjects(a[key], b[key])) return false;
  160. return true;
  161. };
Add Comment
Please, Sign In to add comment