diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
| commit | c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c (patch) | |
| tree | 3e4f42f93c7ae89a364e4d51fff6e5cec4e55fa9 /graphs/js/deepCopyAndEquality | |
add: graphs et rushs
Diffstat (limited to 'graphs/js/deepCopyAndEquality')
| -rw-r--r-- | graphs/js/deepCopyAndEquality/deepCopyAndEquality.js | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/graphs/js/deepCopyAndEquality/deepCopyAndEquality.js b/graphs/js/deepCopyAndEquality/deepCopyAndEquality.js new file mode 100644 index 0000000..4a67b6b --- /dev/null +++ b/graphs/js/deepCopyAndEquality/deepCopyAndEquality.js @@ -0,0 +1,41 @@ +function deepCopy(x) { + if (x == null || x == undefined) { + return x; + } else if (x instanceof Date) { + return new Date(x.valueOf()); + } else if (x instanceof Array) { + const res = new Array(); + + x.forEach((element) => res.push(deepCopy(element))); + return res; + } else if (typeof x === "object") { + const res = {}; + + for (const prop in x) { + res[prop] = deepCopy(x[prop]); + } + + const sort = (o) => + Object.keys(o) + .sort() + .reduce((final, key) => { + final[key] = o[key]; + return final; + }, {}); + + return sort(res); + } else { + return x; + } +} +function deepEquality(a, b) { + if (typeof a !== typeof b) { + return false; + } + + return JSON.stringify(deepCopy(a)) === JSON.stringify(deepCopy(b)); +} +module.exports = { + deepCopy, + deepEquality, +}; |
