-
Notifications
You must be signed in to change notification settings - Fork 0
/
assertObjectsEqual.js
54 lines (50 loc) · 1.66 KB
/
assertObjectsEqual.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const eqArrays = function(arr1, arr2) {
if (arr1.length !== arr2.length) {
return false;
}
for (let i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) {
return false;
}
}
return true;
};
const assertEqual = function(actual, expected) {
if (actual === expected) {
console.log(`✅✅✅ Assertion Passed: ${actual} === ${expected}`);
} else {
console.log(`🛑🛑🛑 Assertion Failed: ${actual} !== ${expected}`);
}
};
// Returns true if both objects have identical keys with identical values.
const eqObjects = function(object1, object2) {
const keys1 = Object.keys(object1);
const keys2 = Object.keys(object2);
// If the objects don't have the same number of keys, they're not equal
if (keys1.length !== keys2.length) {
return false;
}
// Check if all the keys in object1 have a corresponding key with the same value in object2
for (let key of keys1) {
if (Array.isArray(object1[key]) && Array.isArray(object2[key])) {
if (!eqArrays(object1[key], object2[key])) { // key check
return false;
}
} else if (object1[key] !== object2[key]) { // Added value check
return false;
}
}
return true;
};
// FUNCTION IMPLEMENTATION
const assertObjectsEqual = function(actual, expected) {
const inspect = require('util').inspect; // <= add this line
// Implement me!
if (eqObjects(actual, expected)) {
console.log(`✅✅✅ Assertion Passed: ${inspect(actual)} === ${inspect(expected)}`);
} else {
console.log(`🛑🛑🛑 Assertion Failed: ${inspect(actual)} !== ${inspect(expected)}`);
}
};
assertObjectsEqual({a: '1', b: 2 }, {b: 2, a: '1'});
module.exports = assertObjectsEqual;