summaryrefslogtreecommitdiff
path: root/src/utils.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.js')
-rw-r--r--src/utils.js43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/utils.js b/src/utils.js
new file mode 100644
index 0000000..1877a58
--- /dev/null
+++ b/src/utils.js
@@ -0,0 +1,43 @@
+const eq = (a, b) => {
+ if (a === b) {
+ return true;
+ }
+
+ if (a === null || b === null) {
+ return false;
+ }
+
+ if (typeof a != "object" || typeof b != "object") {
+ return false;
+ }
+
+ if (Array.isArray(a) !== Array.isArray(b)) {
+ return false;
+ }
+
+ if (Object.keys(a).length !== Object.keys(b).length) {
+ return false;
+ }
+
+ for (const k in a) {
+ if (!b.hasOwnProperty(k)) {
+ return false;
+ }
+ if (!eq(a[k], b[k])) {
+ return false;
+ }
+ }
+ return true;
+};
+
+const keys = (ks, obj) =>
+ ks.reduce(
+ (ret, k) =>
+ obj.hasOwnProperty(k) ? {...ret, [k]: obj[k]} : ret,
+ {},
+ );
+
+module.exports = {
+ eq,
+ keys,
+};