-
Notifications
You must be signed in to change notification settings - Fork 0
/
assertObjectsEqual.js
68 lines (50 loc) · 1.97 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const eqObjects = function(object1, object2) {
let equivalent = false;
if (Object.keys(object1).length === Object.keys(object2).length) {
for (let property in object1) {
for (let property2 in object2) {
if (property === property2) { //both keys are the same
if (typeof object1[property] === "object" || typeof object2[property] === "object") { //when either obj1 or obj2 value is equal to non-primitive type (array or obj)
if (object1[property].length === object2[property].length) { //if array/ objects are equal length
for (let value of object1[property]) {
for (let value2 of object2[property]) {
if (value === value2) { //if array element is the same
equivalent = true;
} else {
equivalent = false;
}
}
}
} else {
equivalent = false;
}
} else { //when equal to primitive type
if (object1[property] === object2[property]) { //both values are the same
equivalent = true;
} else {
equivalent = false;
}
}
}
}
}
}
return equivalent;
};
const assertObjectsEqual = function(actual, expected) {
const inspect = require('util').inspect;
if (eqObjects(actual, expected)) { //objects are same
console.log(`😜😍🍆Assertion Passed: ${inspect(actual)} === ${inspect(expected)}`);
} else { //objects are different
console.log(`😡🤮🤢Assertion Failed: ${inspect(actual)} !== ${inspect(expected)}`);
}
}
const obj1 = {
val1: ["hi", "hey"],
val2: ["hey", "hola"]
}
const obj2 = {
val1: ["hi", "hey"],
val2: ["hey", "hola"]
}
assertObjectsEqual(obj1, obj2);