-
Notifications
You must be signed in to change notification settings - Fork 0
/
assertObjectsEqual.js
53 lines (44 loc) · 1.49 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
const eqArrays = function(arrOne, arrTwo) {
if(arrOne.length !== arrTwo.length){
return false;
}
for (let i = 0; i < arrOne.length; i++) {
if(Array.isArray(arrOne[i]) && Array.isArray(arrTwo[i])){
if(!eqArrays(arrOne[i], arrTwo[i])){
return false
}
}else if(arrOne[i] !== arrTwo[i]){
return false
}
}
return true
};
// Returns true if both objects have identical keys with identical values.
// Otherwise you get back a big fat false!
const eqObjects = function(object1, object2) {
if(Object.keys(object1).length !== Object.keys(object2).length){
return false
}
for (let key in object1){
if(typeof object1[key] !== typeof object2[key]){
return false
} else if(Array.isArray(object1[key])){
return eqArrays(object1[key], object2[key])
}
}
return true;
}
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)}`)
}
}
// const cd = { c: "1", d: ["2", 3] };
// const dc = { d: ["2", 3], c: "1" };
// assertObjectsEqual(eqObjects(cd, dc), true); // => true
// const cd2 = { c: "1", d: ["2", 3, 4] };
// assertObjectsEqual(eqObjects(cd, cd2), true); // => false
module.exports = assertObjectsEqual