Created
April 10, 2025 11:46
-
-
Save pdaug/6f603e39f30af615e6b68cf89e5bd817 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function countDeepDifferences(a: any, b: any): number { | |
| // Se tipos diferentes, ou valor primitivo diferente | |
| if (typeof a !== typeof b || a === null || b === null) { | |
| return a === b ? 0 : 1; | |
| } | |
| // Comparar arrays | |
| if (Array.isArray(a) && Array.isArray(b)) { | |
| const length = Math.max(a.length, b.length); | |
| let count = 0; | |
| for (let i = 0; i < length; i++) { | |
| count += countDeepDifferences(a[i], b[i]); | |
| } | |
| return count; | |
| } | |
| // Se apenas um for array (e o outro não), é diferente | |
| if (Array.isArray(a) !== Array.isArray(b)) { | |
| return 1; | |
| } | |
| // Comparar objetos | |
| if (typeof a === "object" && typeof b === "object") { | |
| const keys = new Set([...Object.keys(a), ...Object.keys(b)]); | |
| let count = 0; | |
| for (const key of keys) { | |
| const valA = a[key]; | |
| const valB = b[key]; | |
| count += countDeepDifferences(valA, valB); | |
| } | |
| return count; | |
| } | |
| // Valor primitivo | |
| return a === b ? 0 : 1; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment