-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
40 lines (33 loc) · 1.08 KB
/
index.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
const hasOwn = require('./hasOwn');
const isEqual = require('./isEqual');
const isObject = require('./isObject');
const initialResult = {
one: [],
two: [],
};
const deepDiff = function(One, Two, parent, result = initialResult) {
// Get all the keys from the first object
const ObjectOneKeys = Object.keys(One);
ObjectOneKeys.forEach(key => {
// Comparing objects
if (isObject(One[key]) && isObject(Two[key])) {
// If they're empty, return an empty object
if (!Object.keys(One[key]).length && !Object.keys(Two[key]).length) {
return result;
}
deepDiff(One[key], Two[key], key, result);
}
if (hasOwn(Two, key) && !isEqual(One[key], Two[key])) {
// If we're on a recursive call we need to preprend the parent object's name
if (parent) {
result.one.push({ [parent]: { [key]: One[key] } });
result.two.push({ [parent]: { [key]: Two[key] } });
} else {
result.one.push({ [key]: One[key] });
result.two.push({ [key]: Two[key] });
}
}
});
return result;
};
module.exports = deepDiff;