-
Notifications
You must be signed in to change notification settings - Fork 0
/
eqObjects.js
35 lines (30 loc) · 1.07 KB
/
eqObjects.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const assertEqual = require('./assertEqual');
const eqArrays = require('./eqArrays');
const eqObjects = function(object1, object2) {
const keys1 = Object.keys(object1);
const keys2 = Object.keys(object2);
if (keys1.length !== keys2.length) {
return false;
}
for (const key of keys1) {
const value1 = object1[key];
const value2 = object2[key];
if (Array.isArray(value1) && Array.isArray(value2)) {
if (!eqArrays(value1, value2)) {
return false;
}
} else if (value1 !== value2) {
return false;
}
}
return true;
};
module.exports = eqObjects;
/*
Test codes:
const multiColorShirtObject = { colors: ["red", "blue"], size: "medium" };
const anotherMultiColorShirtObject = { size: "medium", colors: ["red", "blue"] };
const longSleeveMultiColorShirtObject = { size: "medium", colors: ["red", "blue"], sleeveLength: "long" };
assertEqual((eqObjects(multiColorShirtObject , anotherMultiColorShirtObject)), true); // => true
assertEqual((eqObjects(multiColorShirtObject , longSleeveMultiColorShirtObject)), false); //=> true
*/