diff options
author | Tor Andersson <tor@ccxvii.net> | 2023-08-12 16:26:00 +0200 |
---|---|---|
committer | Tor Andersson <tor@ccxvii.net> | 2023-08-12 22:38:49 +0200 |
commit | 23ccfae95c9297dbeeb2b8378d1cfea0f96d7e36 (patch) | |
tree | 7bbd91d3e1717e19db0acbdfd193f5742f43942b | |
parent | 0d3133a932665c8864ef0ee3604b9f135081e8fc (diff) | |
download | server-23ccfae95c9297dbeeb2b8378d1cfea0f96d7e36.tar.gz |
Add deep comparison function to utility library.
-rw-r--r-- | public/common/util.js | 26 |
1 files changed, 26 insertions, 0 deletions
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 +} |