-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassertObjectsEqual.js
43 lines (43 loc) · 1.13 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
const inspect = require('util');
// Data
const ab = { a: '1', b: '2', c: [1, 2] };
const ba = { b: '2', a: '1', c: [1, 2] };
const abc = { a: '1', b: '2', c: '3' };
// Array
const eqArrays = (array, match) => {
return (
Array.isArray(array) &&
Array.isArray(match) &&
array.length === match.length &&
array.every((val, index) => val === match[index])
);
};
// Assertion
const assertEqual = function (actual, expected) {
if (actual === expected) {
console.log(`✅✅✅ Assertion Passed: ${actual} === ${expected}`);
} else {
console.log(`🛑🛑🛑 Assertion Failed: ${actual} !== ${expected}`);
}
};
// Main
const eqObjects = (obj1, obj2) => {
if (Object.keys(obj1).length !== Object.keys(obj2).length) return;
for (const key of Object.keys(obj1)) {
for (const k of Object.keys(obj2)) {
if (Array.isArray(key)) {
eqArrays(key, k);
}
if (key === k) {
console.log(
`✅✅✅ Assertion Passed: ${inspect(obj1)} === ${inspect(obj2)}`
);
} else {
console.log(
`🛑🛑🛑 Assertion Failed: ${inspect(obj1)} !== ${inspect(obj2)}`
);
}
}
}
// if (key === Object.values(obj2)) return true;
};