-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (53 loc) · 1.4 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'use strict';
/* eslint-disable array-callback-return, guard-for-in */
const _ = require('lodash.get');
const getDeepKeys = obj => {
let keys = [];
for (const key in obj) {
keys.push(key);
if (typeof obj[key] === 'object') {
const subkeys = getDeepKeys(obj[key]);
keys = keys.concat(subkeys.map(subkey => {
if (typeof key !== 'object') {
return `${key}.${subkey}`;
}
}));
}
}
return keys;
};
const jsonMultilevelDelta = (obj1, obj2) => {
const objects = [getDeepKeys(obj1), getDeepKeys(obj2)];
const deltaKeys = [];
objects.forEach((obj, idx) => {
obj.forEach(key => {
if (!objects[(idx === 0) ? 1 : 0].includes(key)) {
deltaKeys.push(key);
}
});
});
return deltaKeys;
};
const stringToObj = (path, value, obj) => {
const parts = path.split('.');
const last = parts.pop();
let part = null;
while (parts.length) {
part = parts.shift();
if (typeof obj[part] !== 'object') {
obj[part] = {};
}
obj = obj[part];
}
obj[last] = value;
};
module.exports = (obj1, obj2) => jsonMultilevelDelta(obj1, obj2);
module.exports.json = (obj1, obj2) => {
const keys = jsonMultilevelDelta(obj1, obj2);
const jsonObj = {};
keys.forEach(key => {
const value = (typeof _(obj1, key) === 'undefined') ? _(obj2, key) : _(obj1, key);
stringToObj(key, value, jsonObj);
});
return jsonObj;
};