-
Notifications
You must be signed in to change notification settings - Fork 0
/
assertObjectsEqual.js
53 lines (44 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
// Function that compares arrays
const eqArrays = function(array1, array2) {
if (array1.length !== array2.length) return false;
for (i = 0; i < array1.length; i++) {
if (array1[i] !== array2[i]) return false;
}
return true;
}
// Returns true if both objects have identical keys with identical values.
const eqObjects = function(object1, object2) {
const array1 = Object.keys(object1);
const array2 = Object.keys(object2);
// Compares objects length
if (array1.length !== array2.length) return false;
for (let key of array1) {
// use eqArrays function if values are arrays
if (Array.isArray(object1[key]) || Array.isArray(object2[key])) {
return (eqArrays(object1[key], object2[key]));
// If they are not arrays assumes the values are primitives and compare them
} else if (object1[key] !== object2[key]) return false;
}
return true;
};
// function implementation for assertObjectsEqual
const assertObjectsEqual = function(actual, expected) {
const inspect = require('util').inspect;
if (eqObjects(actual, expected))
console.log(`✅✅✅ Assertion Passed: ${inspect(actual)} === ${inspect(expected)}`);
else
console.log(`🛑🛑🛑 Assertion Failed: ${inspect(actual)} !== ${inspect(expected)}`);
};
//TEST CASES
//Primitives as values
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
//Arrays as values
const cd = { c: "1", d: ["2", 3] };
const dc = { d: ["2", 3], c: "1" };
assertObjectsEqual(cd, dc); // => true
const cd2 = { c: "1", d: ["2", 3, 4] };
assertObjectsEqual(cd, cd2); // => false