« get me outta code hell

sugar: compareObjects - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-11-14 16:53:13 -0400
committer(quasar) nebula <qznebula@protonmail.com>2024-11-15 20:40:11 -0400
commit6190ca206d0189fc8d582729efce8ccfbd041e6c (patch)
treee68efa1d0a9cd55dba370f804fbb36a747eed43c /src
parent0bf5694063669734475b39eae393e3c3ee364fbe (diff)
sugar: compareObjects
Diffstat (limited to 'src')
-rw-r--r--src/util/sugar.js28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/util/sugar.js b/src/util/sugar.js
index 3fcf9261..a0c1ed62 100644
--- a/src/util/sugar.js
+++ b/src/util/sugar.js
@@ -216,6 +216,34 @@ export const compareArrays = (arr1, arr2, {checkOrder = true} = {}) =>
     ? arr1.every((x, i) => arr2[i] === x)
     : arr1.every((x) => arr2.includes(x)));
 
+export function compareObjects(obj1, obj2, {
+  checkOrder = false,
+  checkSymbols = true,
+} = {}) {
+  const keys1 = Object.keys(obj1);
+  const keys2 = Object.keys(obj2);
+  if (!compareArrays(keys1, keys2, {checkOrder})) return false;
+
+  let syms1, syms2;
+  if (checkSymbols) {
+    syms1 = Object.getOwnPropertySymbols(obj1);
+    syms2 = Object.getOwnPropertySymbols(obj2);
+    if (!compareArrays(syms1, syms2, {checkOrder})) return false;
+  }
+
+  for (const key of keys1) {
+    if (obj2[key] !== obj1[key]) return false;
+  }
+
+  if (checkSymbols) {
+    for (const sym of syms1) {
+      if (obj2[sym] !== obj1[sym]) return false;
+    }
+  }
+
+  return true;
+}
+
 // Stolen from jq! Which pro8a8ly stole the concept from other places. Nice.
 export const withEntries = (obj, fn) => {
   const result = fn(Object.entries(obj));