-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassertObjectsEqual.js
More file actions
41 lines (32 loc) · 1.1 KB
/
assertObjectsEqual.js
File metadata and controls
41 lines (32 loc) · 1.1 KB
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
36
37
38
39
40
41
//eqObjects function
const eqObjects = function(object1, object2) {
const keysOfObject1 = Object.keys(object1);
const keysOfObject2 = Object.keys(object2);
if (keysOfObject1.length !== keysOfObject2.length) {
return false;
}
for (let key of keysOfObject1) {
if (Array.isArray(object1[key]) && Array.isArray(object2[key])) {
return eqArrays(object1[key, object2[key]]);
} else if (object1[key] !== object2[key]) {
return false;
} else {
return true;
}
}
};
//function that helps us easily test functions that return objects
const assertObjectsEqual = function(actual, expected) {
const inspect = require('util').inspect; //util library's inspect function
if (eqObjects(actual, expected)) {
console.log(`✅ Assertion Passed: ${actual} === ${expected}`);
} else {
console.log(`🛑 Assertion Failed: ${actual} !== ${expected}`);
}
};
module.exports = assertObjectsEqual;
const ab = { a: "1", b: "2" };
const ba = { b: "2", a: "1" };
assertObjectsEqual(ab, ba); // => true
const abc = { a: "1", b: "2", c: "3" };
assertObjectsEqual(ab, abc); // => false