| 1 |
/** |
| 2 |
* Builds a simplified representation of an object, managing complex structures and circular references up to a defined maximum depth. |
| 3 |
* This function is useful for visualizing objects that would otherwise cause errors when converting to a string, such as using `JSON.stringify` |
| 4 |
* on circular structures. |
| 5 |
* |
| 6 |
* @param {Object} obj - The object to represent. Can be a literal object or an array. |
| 7 |
* @param {number} [depth=0] - The current depth of recursive iteration. Defaults to 0. |
| 8 |
* @param {number} [maxDepth=2] - The maximum depth to which the object should be explored. Beyond this depth, objects are not further decomposed. |
| 9 |
* @returns {Object|Array} A simplified representation of the initial object, with nested objects represented up to the specified maximum depth. |
| 10 |
*/ |
| 11 |
function buildRepresentativeObject(obj, depth = 0, maxDepth = 2) { |
| 12 |
if (depth > maxDepth || obj === null || typeof obj !== "object") { |
| 13 |
return obj; |
| 14 |
} |
| 15 |
const result = Array.isArray(obj) ? [] : {}; |
| 16 |
for (const key in obj) { |
| 17 |
if (Object.hasOwn(obj, key)) { |
| 18 |
try { |
| 19 |
const value = obj[key]; |
| 20 |
if (typeof value === "object" && value !== null) { |
| 21 |
result[key] = buildRepresentativeObject(value, depth + 1, maxDepth); |
| 22 |
} else { |
| 23 |
result[key] = value; |
| 24 |
} |
| 25 |
} catch (error) { |
| 26 |
result[key] = `Error: ${error.message}`; |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
return result; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Logs a simplified representation of an object to the console, using `buildRepresentativeObject` to manage complex structures and circular |
| 35 |
* references up to a defined maximum depth. |
| 36 |
* This function is useful for debugging purposes, where directly logging complex objects can be impractical or lead to errors, such as |
| 37 |
* with circular structures. |
| 38 |
* |
| 39 |
* @param {Object} obj - The object to log. Can be a literal object, an array, or any other type that can be represented as an object. |
| 40 |
* @param {number} [depth=0] - The current depth of recursive iteration when building the representative object. Defaults to 0. |
| 41 |
* @param {number} [maxDepth=2] - The maximum depth to which the object should be explored when building the representative object. Beyond this depth, objects are not further decomposed. |
| 42 |
* @returns {void} This function does not return a value; it logs output to the console. |
| 43 |
*/ |
| 44 |
function log(obj, depth = 0, maxDepth = 2) { |
| 45 |
return console.log(buildRepresentativeObject(obj, depth, maxDepth)); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Compares two objects recursively to identify differences between them, handling circular references and limiting comparison depth. |
| 50 |
* This function is particularly useful in debugging scenarios where identifying changes between two states of an object is necessary. |
| 51 |
* |
| 52 |
* @param {Object} obj1 - The first object to compare. |
| 53 |
* @param {Object} obj2 - The second object to compare, ideally representing a later state of the first object to identify changes. |
| 54 |
* @param {Map} [visited=new Map()] - An internal parameter used to track visited objects and manage circular references. It should not be set or modified externally. |
| 55 |
* @param {number} [depth=0] - The current depth of the comparison recursion. Used internally to limit the comparison to a certain depth. |
| 56 |
* @param {number} [maxDepth=10] - The maximum depth to which the comparison should be performed to prevent excessively deep recursion. |
| 57 |
* @returns {Object} An object representing the differences between `obj1` and `obj2`. If objects are identical, an empty object is returned. If a circular reference or maximum depth limit is reached, an error message is included in the return object. |
| 58 |
* |
| 59 |
* @example |
| 60 |
* // Assume `this.pos.getOrder()` returns an order object with its current state. |
| 61 |
* // Capture the state of the order before a certain operation. |
| 62 |
* const beforeBooking = buildRepresentativeObject(this.pos.getOrder()); |
| 63 |
* // Perform an operation that modifies the order. |
| 64 |
* this.pos.getOrder().setBooked(true); |
| 65 |
* // Capture the state of the order after the operation. |
| 66 |
* const afterBooking = buildRepresentativeObject(this.pos.getOrder()); |
| 67 |
* // Compare the before and after states to see what has changed. |
| 68 |
* console.log("Differences: ", compareObjects(beforeBooking, afterBooking)); |
| 69 |
*/ |
| 70 |
function compareObjects(obj1, obj2, visited = new Map(), depth = 0, maxDepth = 10) { |
| 71 |
if (depth > maxDepth) { |
| 72 |
return { error: "Profondeur de comparaison maximale atteinte." }; |
| 73 |
} |
| 74 |
if (visited.has(obj1) || visited.has(obj2)) { |
| 75 |
return visited.get(obj1) === visited.get(obj2) |
| 76 |
? {} |
| 77 |
: { error: "Référence circulaire détectée." }; |
| 78 |
} |
| 79 |
visited.set(obj1, depth); |
| 80 |
visited.set(obj2, depth); |
| 81 |
const differences = {}; |
| 82 |
const allKeys = new Set([...Object.keys(obj1), ...Object.keys(obj2)]); |
| 83 |
allKeys.forEach((key) => { |
| 84 |
const val1 = obj1[key]; |
| 85 |
const val2 = obj2[key]; |
| 86 |
if (typeof val1 !== typeof val2 || val1 !== val2) { |
| 87 |
if (val1 && val2 && typeof val1 === "object" && typeof val2 === "object") { |
| 88 |
const subDiff = compareObjects(val1, val2, visited, depth + 1, maxDepth); |
| 89 |
if (Object.keys(subDiff).length > 0) { |
| 90 |
differences[key] = subDiff; |
| 91 |
} |
| 92 |
} else { |
| 93 |
differences[key] = { obj1: val1, obj2: val2 }; |
| 94 |
} |
| 95 |
} |
| 96 |
}); |
| 97 |
return buildRepresentativeObject(differences); |
| 98 |
} |
| 99 |
|
| 100 |
export const debug = { |
| 101 |
compareObjects, |
| 102 |
buildRepresentativeObject, |
| 103 |
log, |
| 104 |
}; |