Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require('fs');
- // battles were stringified using:
- function stringifyCircular(obj) {
- var cache = [];
- var result = JSON.stringify(obj, (key, value) => {
- if (typeof value === 'object' && value !== null) {
- if (cache.includes(value)) return;
- cache.push(value);
- // Check if the object has a custom toJSON method
- if (typeof value.toJSON === 'function' || typeof value.toObject === 'function') {
- try {
- // Call the custom toJSON method and handle errors if any
- const customJSON = value.toJSON();
- return customJSON;
- } catch (error) {
- // Handle the error gracefully, e.g., by logging it or ignoring it
- console.error(`Error in toJSON method of ${key}: ${error.message}`);
- return undefined; // Skip this object in the resulting JSON
- }
- }
- }
- return value;
- });
- cache = null;
- return result;
- }
- var s = stringifyCircular(window);
- console.log(s.length);
- console.log(s);
- // then, all 3 battles were stored in json files
- var battle1 = require('./battle1.json');
- var battle2 = require('./battle2.json');
- var battle3 = require('./battle3.json');
- function deepCompare(objMe, objDifferent, objSimilar, rootPath = '') {
- const differences = [];
- function compareInternal(currentPath, valueMe, valueDifferent, valueSimilar) {
- // Check if the values are of the same type
- if (typeof valueMe !== typeof valueDifferent) {
- return;
- }
- // Handle primitive types
- if (typeof valueMe !== 'object' || valueMe === null) {
- if (valueMe !== valueDifferent && valueMe === valueSimilar) {
- differences.push({ path: currentPath, value1: valueMe, value2: valueDifferent });
- }
- return;
- }
- // Handle arrays
- if (Array.isArray(valueMe)) {
- if (
- !Array.isArray(valueDifferent) ||
- (valueMe.length !== valueDifferent.length && valueMe.length === valueSimilar.length)
- ) {
- differences.push({ path: currentPath, value1: valueMe, value2: valueDifferent });
- return;
- }
- for (let i = 0; i < valueMe.length; i++) {
- compareInternal(`${currentPath}[${i}]`, valueMe[i], valueDifferent[i], valueSimilar[i]);
- }
- return;
- }
- // Handle objects
- const keysMe = Object.keys(valueMe);
- const keysDifferent = Object.keys(valueDifferent);
- const keysSimilar = Object.keys(valueSimilar);
- for (const key of keysMe) {
- if (!keysDifferent.includes(key) && keysSimilar.includes(key)) {
- differences.push({ path: `${currentPath}.${key}`, value1: valueMe[key], value2: undefined });
- } else {
- compareInternal(`${currentPath}.${key}`, valueMe[key], valueDifferent[key], valueSimilar[key]);
- }
- }
- for (const key of keysDifferent) {
- if (!keysMe.includes(key) && !keysSimilar.includes(key)) {
- differences.push({ path: `${currentPath}.${key}`, value1: undefined, value2: valueDifferent[key] });
- }
- }
- }
- compareInternal(rootPath, objMe, objDifferent, objSimilar);
- return differences;
- }
- // finally, the differences were computed
- var differences = deepCompare(battle1, battle3, battle2);
- differences = differences.filter((d) => typeof d.value1 === typeof d.value2);
- const s = JSON.stringify(differences, null, 4);
- // and saved to a file
- fs.writeFile('differences.json', s, function (err) {
- if (err) return console.log(err);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement