Advertisement
goodwin64

HWM - compare battles

Sep 22nd, 2023
859
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require('fs');
  2.  
  3. // battles were stringified using:
  4. function stringifyCircular(obj) {
  5.   var cache = [];
  6.   var result = JSON.stringify(obj, (key, value) => {
  7.     if (typeof value === 'object' && value !== null) {
  8.       if (cache.includes(value)) return;
  9.       cache.push(value);
  10.  
  11.       // Check if the object has a custom toJSON method
  12.       if (typeof value.toJSON === 'function' || typeof value.toObject === 'function') {
  13.         try {
  14.           // Call the custom toJSON method and handle errors if any
  15.           const customJSON = value.toJSON();
  16.           return customJSON;
  17.         } catch (error) {
  18.           // Handle the error gracefully, e.g., by logging it or ignoring it
  19.           console.error(`Error in toJSON method of ${key}: ${error.message}`);
  20.           return undefined; // Skip this object in the resulting JSON
  21.         }
  22.       }
  23.     }
  24.     return value;
  25.   });
  26.   cache = null;
  27.   return result;
  28. }
  29.  
  30. var s = stringifyCircular(window);
  31. console.log(s.length);
  32. console.log(s);
  33.  
  34. // then, all 3 battles were stored in json files
  35. var battle1 = require('./battle1.json');
  36. var battle2 = require('./battle2.json');
  37. var battle3 = require('./battle3.json');
  38.  
  39. function deepCompare(objMe, objDifferent, objSimilar, rootPath = '') {
  40.   const differences = [];
  41.  
  42.   function compareInternal(currentPath, valueMe, valueDifferent, valueSimilar) {
  43.     // Check if the values are of the same type
  44.     if (typeof valueMe !== typeof valueDifferent) {
  45.       return;
  46.     }
  47.  
  48.     // Handle primitive types
  49.     if (typeof valueMe !== 'object' || valueMe === null) {
  50.       if (valueMe !== valueDifferent && valueMe === valueSimilar) {
  51.         differences.push({ path: currentPath, value1: valueMe, value2: valueDifferent });
  52.       }
  53.       return;
  54.     }
  55.  
  56.     // Handle arrays
  57.     if (Array.isArray(valueMe)) {
  58.       if (
  59.         !Array.isArray(valueDifferent) ||
  60.         (valueMe.length !== valueDifferent.length && valueMe.length === valueSimilar.length)
  61.       ) {
  62.         differences.push({ path: currentPath, value1: valueMe, value2: valueDifferent });
  63.         return;
  64.       }
  65.       for (let i = 0; i < valueMe.length; i++) {
  66.         compareInternal(`${currentPath}[${i}]`, valueMe[i], valueDifferent[i], valueSimilar[i]);
  67.       }
  68.       return;
  69.     }
  70.  
  71.     // Handle objects
  72.     const keysMe = Object.keys(valueMe);
  73.     const keysDifferent = Object.keys(valueDifferent);
  74.     const keysSimilar = Object.keys(valueSimilar);
  75.  
  76.     for (const key of keysMe) {
  77.       if (!keysDifferent.includes(key) && keysSimilar.includes(key)) {
  78.         differences.push({ path: `${currentPath}.${key}`, value1: valueMe[key], value2: undefined });
  79.       } else {
  80.         compareInternal(`${currentPath}.${key}`, valueMe[key], valueDifferent[key], valueSimilar[key]);
  81.       }
  82.     }
  83.  
  84.     for (const key of keysDifferent) {
  85.       if (!keysMe.includes(key) && !keysSimilar.includes(key)) {
  86.         differences.push({ path: `${currentPath}.${key}`, value1: undefined, value2: valueDifferent[key] });
  87.       }
  88.     }
  89.   }
  90.  
  91.   compareInternal(rootPath, objMe, objDifferent, objSimilar);
  92.   return differences;
  93. }
  94.  
  95. // finally, the differences were computed
  96. var differences = deepCompare(battle1, battle3, battle2);
  97. differences = differences.filter((d) => typeof d.value1 === typeof d.value2);
  98. const s = JSON.stringify(differences, null, 4);
  99.  
  100. // and saved to a file
  101. fs.writeFile('differences.json', s, function (err) {
  102.   if (err) return console.log(err);
  103. });
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement