From 23ccfae95c9297dbeeb2b8378d1cfea0f96d7e36 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Sat, 12 Aug 2023 16:26:00 +0200 Subject: Add deep comparison function to utility library. --- public/common/util.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'public') diff --git a/public/common/util.js b/public/common/util.js index 18a78e8..75cd824 100644 --- a/public/common/util.js +++ b/public/common/util.js @@ -278,3 +278,29 @@ function map_delete(map, item) { } } } + +function object_diff(a, b) { + if (a === b) + return false + if (a !== null && b !== null && typeof a === "object" && typeof b === "object") { + if (Array.isArray(a)) { + if (!Array.isArray(b)) + return true + let a_length = a.length + if (b.length !== a_length) + return true + for (let i = 0; i < a_length; ++i) + if (object_diff(a[i], b[i])) + return true + return false + } + for (let key in a) + if (object_diff(a[key], b[key])) + return true + for (let key in b) + if (!(key in a)) + return true + return false + } + return true +} -- cgit v1.2.3